mysql_fetch_assoc() error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

mysql_fetch_assoc() error

Post 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.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
Are you sure that $item (formally know as $results) really holds some values that are found by your sql query?
-
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

Yes.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

each time I see this avatar from pickle (?? sorry), I have to laugh and gain 10 seconds of lifetime :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ha ha - sweet.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Re: mysql_fetch_assoc() error

Post 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
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

I tried removing all the `s, it didn't help. I added or die(mysql_error()) and it did nothing.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply