CodeTalk
CodeTalk

Reputation: 3657

Perform While loop Php

Sorry, this is a bit noobish, I cant get a simple while statement to execute while the condition is true and false if no results:

// Select all the rows in the markers table
$query = "SELECT * FROM markers M, professor P
WHERE P.id = M.id;";
if($row = mysql_fetch_array($result)) {
    do {
        echo $row;
    } 
    while($row = mysql_fetch_array($result));
} else {
    die('No results.');
}

The problem I see with this code is $query is never being called in the mysql_fetch_array() should this be changed?

Upvotes: 0

Views: 97

Answers (2)

Ry-
Ry-

Reputation: 224867

PHP while statements don't have elses. I believe that's nearly unique to Python. Instead, you can do this:

if($row = mysql_fetch_array($result)) {
    do {
        echo $row;
    } while($row = mysql_fetch_array($result));
} else {
    die('No results.');
}

Upvotes: 1

hsz
hsz

Reputation: 152206

Try with following condition:

if ( mysql_num_rows($result) == 0 ) {
  die('No Result');
}

or simplier:

if ( !mysql_num_rows($result) ) {
  die('No Result');
}

Upvotes: 1

Related Questions