Jump to content

Archived

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

slipperyfish

Mysql fetch array error ?

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
[code]
$option = mysql_query("SELECT * FROM toxic_poll_results ORDER BY option_number ASC LIMIT  " .$totaloptions);
[/code]

Share this post


Link to post
Share on other sites
The query should have worked as he had it.... I'm really thinking his $totaloptions variable is empty.

Share this post


Link to post
Share on other sites
ahh thanks. when i used that the mysql eror revealed iwas trying to get out of the table "toxic_poll_results", should have been "toxic_poll_options", silly mistake! Thankyou for your help kind sirS.

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.