Trium918 Posted March 30, 2007 Share Posted March 30, 2007 Could someone please explain why I am getting an endless loop and how to fix it. Thanks. <? $strServer="localhost"; // Server IP Address 'or' Name $strDatabase="mylabser_cms"; // Database Name $strDB=mysql_connect($strServer) or die("Connection to database failed"); $database=mysql_select_db($strDatabase) or die("Database selection Failed"); $query="Select * FROM people"; $result=mysql_query($query); $num=mysql_fetch_array($result) or die("Couldn't run query!!");; echo "<table border=1 width=50% align=center> <tr> <td>Name</td> <td>Age</td> <td>State</td> <td>Sponsor</td> </tr>"; while ($i < $num) { $name=mysql_result($result,$i,"name"); $age=mysql_result($result,$i,"age"); $state=mysql_result($result,$i,"state"); $sponsor=mysql_result($result,$i,"sponsor"); $i++; echo "<tr> <td>$name</td> <td>$age</td> <td>$state</td> <td>$sponsor</td> </tr> </table>"; } ?> Link to comment https://forums.phpfreaks.com/topic/44951-solved-help-with-an-endless-loop/ Share on other sites More sharing options...
obsidian Posted March 30, 2007 Share Posted March 30, 2007 You're referencing $i which is not defined. Also, you are comparing it to $num, but $num contains a record from the query based on your assignment of that variable. Try this: <?php $result = mysql_query($query); $num = mysql_num_rows($result); if ($num > 0) { for ($i = 0; $i < $num; $i++) { $name = mysql_result($result,$i,"name"); $age = mysql_result($result,$i,"age"); $state = mysql_result($result,$i,"state"); $sponsor = mysql_result($result,$i,"sponsor"); // Display here } } ?> Good luck Link to comment https://forums.phpfreaks.com/topic/44951-solved-help-with-an-endless-loop/#findComment-218251 Share on other sites More sharing options...
Trium918 Posted March 30, 2007 Author Share Posted March 30, 2007 I got both methods to work. It was the mysql_fetch_array. When should I use the mysql_fetch_array? The While Loop $result=mysql_query($query); $num=mysql_num_rows($result) or die("Couldn't run query!!"); $i =0; while ($i < $num) { $name=mysql_result($result,$i,"name"); $age=mysql_result($result,$i,"age"); $state=mysql_result($result,$i,"state"); $sponsor=mysql_result($result,$i,"sponsor"); $i++; echo "<tr> td>$name</td> <td>$age</td> <td>$state</td> <td>$sponsor</td> </tr> </table>"; }// End of while loop [color=red] The For Loop[/color] $result = mysql_query($query); $num = mysql_num_rows($result); if ($num > 0) { for ($i = 0; $i < $num; $i++) { $name = mysql_result($result,$i,"name"); $age = mysql_result($result,$i,"age"); $state = mysql_result($result,$i,"state"); $sponsor = mysql_result($result,$i,"sponsor"); // Output echo "<tr> <td>$name</td> <td>$age</td> <td>$state</td> <td>$sponsor</td> </tr> </table>"; }// End of For loop } Link to comment https://forums.phpfreaks.com/topic/44951-solved-help-with-an-endless-loop/#findComment-218261 Share on other sites More sharing options...
obsidian Posted March 30, 2007 Share Posted March 30, 2007 It works, but what does mysql_fetch_array() do? That is the command for retrieving one row of the query. You will typically see that used in a loop such as this: <?php $result = mysql_query($query); if ($num > 0) { while ($row = mysql_fetch_array($result)) { $name = $row['name']; $age = $row['age']; $state = $row['state']; $sponsor = $row['sponsor']; // Display here } } ?> Check out more in the manual: mysql_fetch_array() Link to comment https://forums.phpfreaks.com/topic/44951-solved-help-with-an-endless-loop/#findComment-218267 Share on other sites More sharing options...
Trium918 Posted March 30, 2007 Author Share Posted March 30, 2007 Why would I need the if() statement, or why would you use it because it work without it? if ($num > 0) { for ($i = 0; $i < $num; $i++) { $name = mysql_result($result,$i,"name"); $age = mysql_result($result,$i,"age"); $state = mysql_result($result,$i,"state"); $sponsor = mysql_result($result,$i,"sponsor"); // Output echo "<tr> <td>$name</td> <td>$age</td> <td>$state</td> <td>$sponsor</td> </tr> </table>"; }// End of For loop } Link to comment https://forums.phpfreaks.com/topic/44951-solved-help-with-an-endless-loop/#findComment-218272 Share on other sites More sharing options...
obsidian Posted March 30, 2007 Share Posted March 30, 2007 The if statement simply allows you to check if there were any rows returned before you try to loop through the results. If not, it also allows you to echo a remark stating this to the user. Link to comment https://forums.phpfreaks.com/topic/44951-solved-help-with-an-endless-loop/#findComment-218276 Share on other sites More sharing options...
Trium918 Posted March 30, 2007 Author Share Posted March 30, 2007 I have another problem. Ok, correct me if I am wrong but the for loop are any loop runs until it reads every line in the table. I would asume that it is but it isn't placing every record in a table. Only the first person. echo "<tr> <td>$name</td> <td>$age</td> <td>$state</td> td>$sponsor</td> </tr> </table>"; Link to comment https://forums.phpfreaks.com/topic/44951-solved-help-with-an-endless-loop/#findComment-218281 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.