Jump to content

Recommended Posts

<?php
mysql_connect("localhost", "root", "password") or die(mysql_error);
mysql_select_db("tutorial1") or die(mysql_error);


echo "<table border='1'>";
echo "<tr><th>Name</th><th>Age</th><th>Sex</th></tr>";

while($personalinfo = mysql_fetch_array(mysql_query("SELECT * FROM personalinfo")) or die(mysql_error())){

echo "<tr><td>";
echo $personalinfo['Name'];
echo "</td><td>";
echo $personalinfo['Age'];
echo "</td><td>"; 
echo $personalinfo['Sex'];
echo "</td></tr>"; 
}
echo "</table>";
?>

 

im trying to make it say it once, and if there another different one that it goes through that and if there is no more after that, to stop. But it just goes on, until a 30 sec error.

 

Link to comment
https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/
Share on other sites

maybe something like this

 


<?php

$result = mysql_query("SELECT * FROM personalinfo");
if (!$result) { die(mysql_error()); }

$dbRows = mysql_num_rows($result);

if ($dbRows > 0) {

echo "<table border='1'>";
echo "<tr><th>Name</th><th>Age</th><th>Sex</th></tr>";

while ($perinfo = mysql_fetch_assoc($result)) {

echo "<tr><td>";
echo $personalinfo['Name'];
echo "</td><td>";
echo $personalinfo['Age'];
echo "</td><td>"; 
echo $personalinfo['Sex'];
echo "</td></tr>"; 
}
echo "</table>";
} else {
echo "Sorry. No info found.";
}
?>

mysql_num_rows counts the number of rows the query uses to produce the query

 

So technically if no rows are returned you have an empty result which means there is no need to do the while stuff.

 

The reason your solution did not work was because the mysql_query() was re-generating the query in your while loop, so it would only go to the first record. Where as clown's solution the mysql_query is assigned to the variable and is not re-generated each time the loop is ran.

 

Hope that helps.

mysql_connect("localhost", "root", "password") or die(mysql_error);
mysql_select_db("tutorial1") or die(mysql_error);

$sqlquery = mysql_query("SELECT * FROM personalinfo");

if (!$sqlquery) { die(mysql_error());}

$dbrows = mysql_num_rows($result);
if ($dbrows > 0) { 
echo "<table border='1'>";
echo "<tr><th>Name</th><th>Age</th><th>Sex</th></tr>";
}
else { 
echo "Data is not available at this time. Please try again later.";

while($personalinfo = mysql_fetch_assoc($sqlquery)){

echo "<tr><td>";
echo $personalinfo['Name'];
echo "</td><td>";
echo $personalinfo['Age'];
echo "</td><td>"; 
echo $personalinfo['Sex'];
echo "</td></tr>"; 
echo "</table>";
}

 

 

got a $end error which is normally a punctuation error.

else { 
echo "Data is not available at this time. Please try again later.";

while($personalinfo = mysql_fetch_assoc($sqlquery)){

Theres no ending }

Should be:

else { 
echo "Data is not available at this time. Please try again later.";
}
while($personalinfo = mysql_fetch_assoc($sqlquery)){

bump

yea that was embarrassing im a tad sleepy.

 

last question

 

for some reason my first is in the table, but the second is not. its outside of the table...

 

<?php

while($personalinfo = mysql_fetch_assoc($sqlquery)){

echo "<tr><td>";
echo $personalinfo['Name'];
echo "</td><td>";
echo $personalinfo['Age'];
echo "</td><td>"; 
echo $personalinfo['Sex'];
echo "</td></tr>"; 
echo "</table>"; //<---- THIS IS WHY
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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