Jump to content

Warning: mysql_fetch_assoc not a valid MySQL result


KittyKate

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]
Link to comment
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
Link to comment
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
Link to comment
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.