Jump to content

[SOLVED] Help with an endless loop.


Trium918

Recommended Posts

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

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

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
}

 

 

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()

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
}

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>";

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.