Jump to content


Photo

Mysql fetch array error ?


  • Please log in to reply
4 replies to this topic

#1 slipperyfish

slipperyfish
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 06 June 2006 - 05:17 PM

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:


<?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);

?>


can any body help?

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 06 June 2006 - 05:24 PM

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:
$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++;

    }
To this:
$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();
    }

Run that and tell us what it says.

Info: PHP Manual


#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 06 June 2006 - 05:26 PM

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

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 06 June 2006 - 05:28 PM

The query should have worked as he had it.... I'm really thinking his $totaloptions variable is empty.

Info: PHP Manual


#5 slipperyfish

slipperyfish
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 06 June 2006 - 05:30 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users