Page 1 of 1

mysql_fetch_assoc() error

Posted: Thu Jun 30, 2005 1:36 pm
by cbrian
Here's a little piece of the code.

Code: Select all

$item = mysql_query("SELECT `id`, `name`, `rarity1`, `rarity2` FROM `items` WHERE `level`='".$mon['level']."'");
$irand = rand(1,1000);
while(($item = mysql_fetch_assoc($item)) && (!$inews)) {
This gives me a
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/stlawren/www/cbc/KoD/game/explore.php on line 129
error. I can't find anything wrong with the query - and when I comment out the while loop and add a mysql_fetch_assoc($item) above it, I don't get any problem.

Posted: Thu Jun 30, 2005 1:56 pm
by djot
-
Are you sure that $item (formally know as $results) really holds some values that are found by your sql query?
-

Posted: Thu Jun 30, 2005 2:25 pm
by cbrian
Yes.

Posted: Thu Jun 30, 2005 2:49 pm
by djot

Code: Select all

$sql="SELECT `id`, `name`, `rarity1`, `rarity2` FROM `items` WHERE `level`='".$mon['level']."'";
$result = mysql_query($sql);
//$irand = rand(1,1000);

while($item = mysql_fetch_assoc($result)) {
  echo "ID: ".$item['id']." | name: ".$item['name']."<br>";
  if ($item['xyz_dontknow'] == $inews)
  {
    //do something
    echo "match!<hr>";
  }
}
mysql_free_result($result);

Posted: Thu Jun 30, 2005 2:56 pm
by pickle
try tacking on an 'or die(mysql_error());' line on the end of your query line. That'll output any mysql error that happen. Also, echo the query before its executed and run what's output. You might get something you're not expecting.

Posted: Thu Jun 30, 2005 2:59 pm
by djot
each time I see this avatar from pickle (?? sorry), I have to laugh and gain 10 seconds of lifetime :)

Posted: Thu Jun 30, 2005 3:01 pm
by pickle
Ha ha - sweet.

Re: mysql_fetch_assoc() error

Posted: Fri Jul 01, 2005 5:16 am
by harrisonad
cbrian wrote:"SELECT `id`, `name`, `rarity1`, `rarity2` FROM `items` WHERE `level`='".$mon['level']."'"
Remove all (`) from column and table names as in...

Code: Select all

"SELECT id, name, rarity1, rarity2 FROM items WHERE level='$mon[level]'"
I also included the $mon['level'] inside the string to make it readable.
If the error persists just let us know

Posted: Fri Jul 01, 2005 7:54 am
by cbrian
I tried removing all the `s, it didn't help. I added or die(mysql_error()) and it did nothing.

Posted: Tue Jul 05, 2005 10:03 am
by pickle
In your initial code posting, you name your resultset 'item', then overwrite that variable each time you loop. ~djot renamed your resultset 'result'. Did you make the change that ~djot suggested? Could you post your updated code?