Jump to content

Mysql fetch array error ?


slipperyfish

Recommended Posts

Hey all. Im having a little problem creating a poll. Im running some mySQL functions, and i keep getting this error message:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/fhlinux186/t/toxicgaming.newbiestyle.co.uk/user/htdocs/postpoll.php on line 54

[/quote]

Here's the code im running relative to that area:

[code]

<?php

$db = mysql_connect("****", "****", "***") or die("Could Not Connect");
if(!$db)
    die("no db");
if(!mysql_select_db("******",$db))
    die("No database selected.");

$totaloptions = "5";

$option = mysql_query("SELECT * FROM toxic_poll_results ORDER BY option_number ASC LIMIT " .$totaloptions. "");
    
    while($optionarray=mysql_fetch_assoc($option)) {

        $optionvotes = $optionarray['option_votes'];
        $percent = ($totalvotes / 100) * $optionvotes;

        print '' .$option[$x]. ': <img src="images/bar.gif" width="' .$percent. '" /> ' .$percent. '%<br /><br />';

        $x++;

    }

mysql_close($db);

?>

[/code]

can any body help?
Link to comment
https://forums.phpfreaks.com/topic/11346-mysql-fetch-array-error/
Share on other sites

You'll find that the error you're getting often results from a bad SQL query, like your value for the limit isn't correct or something.

Change this:
[code]$option = mysql_query("SELECT * FROM toxic_poll_results ORDER BY option_number ASC LIMIT " .$totaloptions. "");
    
    while($optionarray=mysql_fetch_assoc($option)) {

        $optionvotes = $optionarray['option_votes'];
        $percent = ($totalvotes / 100) * $optionvotes;

        print '' .$option[$x]. ': <img src="images/bar.gif" width="' .$percent. '" /> ' .$percent. '%<br /><br />';

        $x++;

    }[/code]
To this:
[code]$option = mysql_query("SELECT * FROM toxic_poll_results ORDER BY option_number ASC LIMIT " .$totaloptions. "");
if($option && mysql_num_rows($option) > 0)
{    
    while($optionarray=mysql_fetch_assoc($option)) {

        $optionvotes = $optionarray['option_votes'];
        $percent = ($totalvotes / 100) * $optionvotes;

        print '' .$option[$x]. ': <img src="images/bar.gif" width="' .$percent. '" /> ' .$percent. '%<br /><br />';

        $x++;
}
else
      echo mysql_error();
    }[/code]

Run that and tell us what it says.

Archived

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

×
×
  • 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.