Jump to content

Problem with a while loop...


provision

Recommended Posts

Hi,

 

I have written this code to make a table from a query but it does not seem to be working. It does nothing, just a blank page.

 

Here is the code...

 

$sql = "select movie_name, movie_director, genre from movies 
order by date_enterd desc limit 10";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {
$title = $row['movie_name'];
$director = $row['movie_director'];
$genre = $row['genre'];


print "<table id='movies'>
<th>Title</th>
<th>Director</th>
<th>Genre</th>
";

print"<tr>
<td>$title</td>
<td>$director</td>
<td>$genre</td>
</tr>";
};
print"</table>";

 

What is wrong with it?

 

Thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/113897-problem-with-a-while-loop/
Share on other sites

I think its a syntax error

please check your error log

 

you are putting a semi colon after the while closing.

 

please check if it works this way

 

$sql = "select movie_name, movie_director, genre from movies

order by date_enterd desc limit 10";

 

$result = mysql_query($sql);

echo "<table id='movies'>

<th>Title</th>

<th>Director</th>

<th>Genre</th> ";

/* see to it that everything is on the same line */

 

while($row = mysql_fetch_array($result)) {

$title = $row['movie_name'];

$director = $row['movie_director'];

$genre = $row['genre'];

 

print"<tr>

<td>$title</td>

<td>$director</td>

<td>$genre</td>

</tr>";

}

print"</table>";

You really need to check your query succeeds and returns data before trying to use it. Also, you need to move the opening <table> tag out of the loop.

 

<?php

$sql = "select movie_name, movie_director, genre from movies order by date_enterd desc limit 10";

if ($result = mysql_query($sql)) {
 if (mysql_num_rows($result)) {
   print "<table id='movies'>";
   while ($row = mysql_fetch_array($result)) {
     $title = $row['movie_name'];
     $director = $row['movie_director'];
     $genre = $row['genre'];
     print "
     <th>Title</th>
     <th>Director</th>
     <th>Genre</th>
     <tr>
     <td>$title</td>
     <td>$director</td>
     <td>$genre</td>
     </tr>";
   }
   print "</table>";
 } else {
   echo "No results found";
 }
} else {
 echo "Query failed<br />$sql<br />" . mysql_error();
}

?>

Guest Xanza
Although they do basically the same thing, print() can return a true or false value, and echo() can not. This means that print() could be utilized in some situations echo() can not, but in a very very long program, echo() will run slightly faster. So yes, depending on your situation one could be more useful than the other.... but in most circumstances, they both do the same job.

 

I suggested he use echo() because he's not trying to gain a Boolean response from his script, but not only that, I've read on php.net that echo() is slightly faster compared to print(). But I'm sure it's insignificant. But I'm sure either way their is barely a difference, but from my experience, I've only ever had problems with print() while being a php noobie. :) So just a suggestion.

he's not trying to gain a Boolean response from his script

 

print returns 1 always, so there's not much point in testing its return value anyways, we already know what it will be.

 

I've read on php.net that echo() is slightly faster compared to print()

 

Yeah, this is another of those age old arguments, honestly, its not worth talking/thinking about.

 

The only real difference between print and echo is the fact that echo excepts multiple arguments, print does not. Whats the point when there is a concatenation operator in both that does the same thing?

 

Anyway, its personal preference I guess. I use echo myself, just wanted to know why you thought it might be useful to try the script using echo instead of print.

Archived

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

×
×
  • Create New...

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.