PHP: Looping

They have: 71 posts

Joined: Aug 2001

Okay basically I'm getting an error and I don't know why. Maybe you guys can help me?

here is my code:

<?php
while($row = mysql_fetch_array($news))  {

// stuff goes here

}
?>

So what's wrong with it? It's the while line that is causing the error but I can't see a thing wrong with it, can you? I have also tried using the do while loop but it prints the same error for the while bit on that too.

Yes my database connections are correct Smiling

Thanks in advance,

Adam.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

What is the error?
Is $news a valid mySQL result?

They have: 71 posts

Joined: Aug 2001

Yes news is valid.

Warning: Supplied argument is not a valid MySQL result resource in C:\apache\server\htdocs/agNews/read.php on line 8

that is the error

here is the line above the while command..

<?php
$news
= mysql_query(\"SELECT * FROM news ORDER BY date DESC LIMIT 8;\") || die(\"Cannot select records\");
?>

Is it my query? It's the only other thing that I can think of..

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

No, that error tells you that $news is not valid.

This should give you more debuging help:

<?php
$query
= \"SELECT * FROM news ORDER BY date DESC LIMIT 8\";
$result = mysql_query($query);
if (!
$result) {
    echo \"\n\n\";
    echo \"<!--\n\";
    echo \"FILE: \" . __FILE__ . \"\n\";
    echo \"LINE: \" . __LINE__ . \"\n\";
    echo \"\n\";
    echo \"QUERY ERROR:\n\";
    echo mysql_error() . \"\n\";
    echo \"\n\";
    echo \"QUERY:\n\";
    echo
$query . \"\n\";
    echo \"-->\n\";
    echo \"\n\";
}
while (
$result && $row=mysql_fetch_array($result)) {
    // do something
}
?>
This should print the file and approximate line number where the faulty query was executed. It will also print the mySQL error and faulty query.

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.