View Full Version : mysql_fetch_array Problem

02-07-2006, 10:18 PM
When I run this script It says Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in blah blah....

include "db.php";
$username = $_SESSION['username'];
$result = mysql_query("SELECT * FROM users WHERE to='$username'");
while ($row = mysql_fetch_array($result)) { ?>

From: <? $row['from'] ?><br>
Subject: <? $row['subject'] ?><br>
Message: <? $row['message'] ?><br>
<? } ?>

Kid Charming
02-07-2006, 10:34 PM
Your query is failing, so it's returning FALSE. Read more about mysql_query() return values here (http://us3.php.net/manual/en/function.mysql-query.php) and how to get the database's error message with the mysql_error() function here (http://us3.php.net/manual/en/function.mysql-error.php).

02-08-2006, 10:30 AM
to help us find the problem could you do this.

$result = mysql_query("SELECT * FROM users WHERE to='$username'") or die(mysql_error());

and post the results.

the mysql_error will show you if you are getting an error from the query.

you could even try this

$query = "SELECT * FROM users WHERE to='$username'";
print $query;
$result = mysql_query($query) or die(mysql_error());

this will then let us see if $username is being set.



02-08-2006, 05:48 PM
When I try the below it I get 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to=''' at line 1'

$result = mysql_query("SELECT * FROM users WHERE to='$username'") or die(mysql_error());

Kid Charming
02-08-2006, 05:52 PM
TO is a reserved word in MySQL. You can continue to use it as a column name, but you'll need to enclose it in backticks (`to`='$username') when you refer to it. I would recommend renaming it, though.

02-08-2006, 05:55 PM
Now it says Unknown column 'to' in 'where clause'

Kid Charming
02-08-2006, 06:00 PM
Are you sure there's a column called 'to' in the users table?

02-08-2006, 06:01 PM
Is column the same thing as a field? If so there is one called that.

Kid Charming
02-08-2006, 06:04 PM
Please run a SHOW CREATE TABLE statement on your users table and post the result, along with the exact query you're sending.