Jump to content


Photo

Warning: mysql_fetch_assoc not a valid MySQL result


  • Please log in to reply
3 replies to this topic

#1 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 26 July 2006 - 11:01 PM

I'm getting the warning:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

from the following code and don't know why. According the MySQL Query Browser the query works, so what is up with the instruction? I essentially grabbed the code right from: http://ca.php.net/ma...mysql-query.php

<?php
$query = sprintf("SELECT Keyword FROM SFM_Project_Keywords WHERE PI_Word = %i AND Code = '%s'", $pi, $_GET['code']);
$result = mysql_query($query);
       
$keywords = "";
       
while ($row = mysql_fetch_assoc($result)) {
    $keywords .= $row['Keyword'] . " :: ";
}
?>

PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 26 July 2006 - 11:04 PM

one trick to debug queries: mysql_error().

$result = mysql_query($query) or die(mysql_error());

another good way is to echo the query to screen, so you see exactly what went wrong. sometimes manually typing a query into mysql browser and the actual query generated from php are different.

From what I see here you are probably missing the quotes around %i
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 scheols

scheols
  • Members
  • PipPipPip
  • Advanced Member
  • 113 posts

Posted 27 July 2006 - 03:08 AM

<?php
$query = sprintf("SELECT Keyword FROM SFM_Project_Keywords WHERE PI_Word = %i AND Code = '%s'", $pi, $_GET['code']);
$result = mysql_query($query);
       
$keywords = "";
       
while ($row = mysql_fetch_array($result)) {
    $keywords .= $row['Keyword'] . " :: ";
}
?>
try that
Dont Piss me Off today

#4 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 27 July 2006 - 03:21 PM

Turns out fprints() didn't like the %i. Switching it up to a %s didn't work, but putting $pi right into the string fixed the error. Since the value of $pi is set within the script I'm not worried about SQL injection attacks. The query string now looks like:

$query = sprintf("SELECT Keyword FROM SFM_Project_Keywords WHERE PI_Word = $pi AND Code = '%s'", mysql_real_escape_string($_GET['code']));

Thanks for the help!
PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users