Log in

View Full Version : mysql_fetch_array(): supplied argument . . .



Spiterz
08-30-2006, 12:04 PM
<?php
if (isset($_COOKIE["login"])) {

$con = mysql_connect("localhost","example","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("example_database", $con);

$result = mysql_query("SELECT * FROM `users`
WHERE `username`=$_COOKIE[login]");

while($row = mysql_fetch_array($result))
{
$cash = $row['money'] - '25';
echo 'Cash: ' . $cash . '<br> <a href=test.php> Back to the pencil Shop! </a>';
}


mysql_query("UPDATE `users` SET `money` = '$cash'
WHERE `username` = '$_COOKIE[login]'");

mysql_close($con);



} else {
echo "Welcome guest!<br />";
}
?>



Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/spiterz2/domains/battlecraft.cankoo.com/public_html/buy.php on line 15

Thats my code, and underneath is the error i get =S I cant solve it.

Strangeplant
08-30-2006, 12:38 PM
For one thing, looks to me that $result is probably a null string, returned because the initial look-up failed. You should probably test $result to make sure that the cookie is valid anyways. The error probably ocurred because of missing quotes:
$result = mysql_query("SELECT * FROM `users`
WHERE `username`=$_COOKIE[login]");should be
$result = mysql_query("SELECT * FROM users WHERE username = $_COOKIE['login']");And don't use backticks around the MySQL variables. The quotes are missing in a line farther down as well.....

And there could be more.....

Twey
08-30-2006, 01:00 PM
$result = mysql_query("SELECT * FROM users WHERE username = $_COOKIE['login']");Should be:
$result = mysql_query("SELECT * FROM users WHERE username = {$_COOKIE['login']}");When using complex variables in double quotes, you must use braces to distinguish the variable from the rest of the string.

Strangeplant
08-30-2006, 01:17 PM
Oops! Forgot that important one....