Jump to content


Photo

MySQL_fetch_array error :S


  • Please log in to reply
6 replies to this topic

#1 slipperyfish

slipperyfish
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 26 March 2006 - 03:14 PM

Hey! Here is my PHP code:

<?php

[CONNECT INFO]

$feedbackSQL = "SELECT * FROM feedback ORDER BY time ASC LIMIT 10";
    $feedback = mysql_query($feedbackSQL) or die("Error communicating with database.");

    If ($feedback) {

        while($feedbackInfo=mysql_fetch_array($feedback)) {
 
            $feedbackName = $feedbackInfo[feedbackName];
            $feedbackEmail = $feedbackInfo[feedbackEmail];
            $feedback = $feedbackInfo[feedback];
            
                      print "<u>Feedback</u><br /><b>Name:</b> " .$feedbackName. " - <b>Email:</b> " .$feedbackEmail. "<br />" .$feedback. "";

        }
              

    // Display numbers

        $totalSQL = "SELECT COUNT(*) FROM feedback";
            $totalRun = mysql_query($totalSQL);
            $total = mysql_fetch_array($totalRun);

        If ($total > "10") {
            $total = abs($total);
            $pages = ($total/10);
            
            $x = 1;
            while($x<$pages){
                echo $x;
                $x++;
            }

        }    



    } else {

    print "No feedback, soz boz!";

    }

?>

Thats my work in progress .. however it, returns the first result form the db, then underneath has this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/fhlinux218/n/newbiestyle.co.uk/user/htdocs/help/feedback.php on line 14

.. confused I am, here's the script's URL:

[a href=\"http://www.newbiestyle.co.uk/help/feedback.php\" target=\"_blank\"]http://www.newbiestyle.co.uk/help/feedback.php[/a]



Help much appreciated..



#2 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 26 March 2006 - 04:08 PM

My guess is that you can't use mysql_fetch_array on a query that uses count.
Try $total = mysql_result($totalRun, 0, 0);
Personal site: NewEnigma  |  Java and JSF Discussion

#3 slipperyfish

slipperyfish
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 26 March 2006 - 04:17 PM

still not working :S

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 26 March 2006 - 04:20 PM

You are reusing your pointer to the results. You can't do that and expect it to work.
Your code:
<?php
    $feedback = mysql_query($feedbackSQL) or die("Error communicating with database.");

    If ($feedback) {

        while($feedbackInfo=mysql_fetch_array($feedback)) {

            $feedbackName = $feedbackInfo[feedbackName];
            $feedbackEmail = $feedbackInfo[feedbackEmail];
            $feedback = $feedbackInfo[feedback]; // here's the problem
?>
The fix:
<?php
    $rs = mysql_query($feedbackSQL) or die('Problem with query: ' . $feedbackSQL . '<br>' . mysql_error()); // don't need long varibale names for query results. If you're going to use the "or die" clause, make the message it puts out meaningful so you can fix the error.
 
    If ($rs) {

        while($rw=mysql_fetch_assoc($feedback)) { // again, why the long variable name

            $feedbackName = $rw['feedbackName'];
            $feedbackEmail = $rw['feedbackEmail'];
            $feedback = $rw['feedback']; // now there is no conflict on the variable names
?>
See the comments in the above code. You will have to change the rest of your code appropriately.

Ken

#5 slipperyfish

slipperyfish
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 26 March 2006 - 04:38 PM

ok, iv done what you put ken.. this is my new code:

<?php

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

$feedbackSQL = "SELECT * FROM feedback";
    $rs = mysql_query($feedbackSQL) or die('Problem with query: ' . $feedbackSQL . '<br>' . mysql_error());

    If ($rs) {

        while($rw=mysql_fetch_assoc($feedback)) {
 
            $feedbackName = $rw['feedbackName'];
            $feedbackEmail = $rw['feedbackEmail'];
            $feedback = $rw['feedback'];
            
                      print "<u>Feedback</u><br /><b>Name:</b> " .$feedbackName. " - <b>Email:</b> " .$feedbackEmail. "<br />" .$feedback. "";

        }
              
    } else {

    print "No feedback, soz boz!";

    }

?>

you can view this script:
[a href=\"http://www.newbiestyle.co.uk/help/feedback.php\" target=\"_blank\"]http://www.newbiestyle.co.uk/help/feedback.php[/a]

Im now getting the following error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/fhlinux218/n/newbiestyle.co.uk/user/htdocs/help/feedback.php on line 14


so confused :S

help appreciated!

#6 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 26 March 2006 - 04:44 PM

while($rw=mysql_fetch_assoc($feedback)) {

replace $feedback with $rs

Personal site: NewEnigma  |  Java and JSF Discussion

#7 slipperyfish

slipperyfish
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 26 March 2006 - 04:45 PM

luv ya guys! thanks for your help!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users