Jump to content

Archived

This topic is now archived and is closed to further replies.

KittyKate

Warning: mysql_fetch_assoc not a valid MySQL result

Recommended Posts

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/manual/en/function.mysql-query.php

[code]<?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'] . " :: ";
}
?>[/code]

Share this post


Link to post
Share on other sites
one trick to debug queries: mysql_error().

[code]$result = mysql_query($query) or die(mysql_error());[/code]

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

Share this post


Link to post
Share on other sites
[code=php:0]<?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'] . " :: ";
}
?>[/code] try that

Share this post


Link to post
Share on other sites
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:

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

Thanks for the help!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.