Jump to content

Archived

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

slipperyfish

MySQL_fetch_array error :S

Recommended Posts

Hey! Here is my PHP code:

[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!";

    }

?>[/code]

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

Share this post


Link to post
Share on other sites
My guess is that you can't use mysql_fetch_array on a query that uses count.
Try $total = mysql_result($totalRun, 0, 0);

Share this post


Link to post
Share on other sites
You are reusing your pointer to the results. You can't do that and expect it to work.
Your code:
[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
?>[/code]
The fix:
[code]<?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
?>[/code]
See the comments in the above code. You will have to change the rest of your code appropriately.

Ken

Share this post


Link to post
Share on other sites
ok, iv done what you put ken.. this is my new code:

[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!";

    }

?>
[/code]

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!

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.