14

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

simple question here.

I have a SELECT query

SELECT FROM friendzone WHERE ID = '$editID'"

I am sure this is going to give me only one row as result, cause ID's can't be duplicate in my DB.

How can I access the column values?

$row = mysql_fetch_array($query);

I think this is useless since I don't have to make any array.. I have only one row!

If I don't put it into a While cicle, and try to do e.g.

.$row['ID'].

I get:

mysql_fetch_array() expects parameter 1 to be resource, boolean given

Thanks in advance everyone.

3
  • Are you trying to select a row with a specific id? If so do you want it to always select that row or do you want it done dynamically? Commented Dec 9, 2012 at 20:33
  • 3
    Your SELECT statement is invalid - you have no columns specified or SELECT * Your query fails, and you don't do any error checking. Commented Dec 9, 2012 at 20:34
  • I feel so stupid. Thanks Michael. Commented Dec 9, 2012 at 20:36

3 Answers 3

18

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Try with:

$query = mysql_query("SELECT * FROM friendzone WHERE ID = '$editID'");
$row = mysql_fetch_array($query);

print_r($row);

MySQLi code:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli('host', 'username', 'password', 'database');
$stmt = $conn->prepare("SELECT * FROM friendzone WHERE ID = ?");
$stmt->bind_param("s", $editID);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

print_r($row);
Sign up to request clarification or add additional context in comments.

3 Comments

Obviously the SQL Query was done with the code you posted
+1. Please also provide the mysqli and/or PDO code to do it, as to have no excuse for not doing it.
Thank you! I have chosen MySQLi and using it now :)
3

Probably your $query is equal false because something went wrong, try mysql_error() to see whats wrong.

And 2 small advices:

  1. would be better to use PDO od mysqli as mysql_* functions are deprecated.

  2. use at least mysql_real_escape_string() to escape the value before putting it into SQL string

Comments

-2

Since I don't know what columns your trying to select the general syntax for select is

"SELECT column1, column2, column3 FROM friendzone WHERE ID ='$editID'"

Or if you want to select all columns just type

"SELECT * FROM friendzone WHERE ID = '$editID'"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.