Jump to content

Recommended Posts

   <?php
   
   mysql_connect($dbhost, $dbuser, $dbpass);
   mysql_select_db($dbactual);
   
   $query = "SELECT * FROM logs WHERE level = 8";
   $result = mysql_query($query);
   $number = mysql_num_rows($result);
   $i = 0;
   
   echo "<table><tr>";
   echo "<td><strong>Log ID</strong></td><td><strong>User</strong></td><td><strong>User Level</strong></td><td><strong>Date</strong></td><td><strong>Time</strong></td><td><strong>Action</strong></td></tr>";
   
   while ($i < $number)
{
$id = mysql_result($result,$i,"id");
$user_id = mysql_result($result,$i,"user_id");
$level = mysql_result($result,$i,"level");
$date = mysql_result($result,$i,"date");
$time = mysql_result($result,$i,"time");
$action = mysql_result($result,$i,"action");

echo "<tr><td>$id</td><td>$user_id</td><td>$level</td><td>$date</td><td>$time</td><td>$action</td></tr>";

}

echo "</table>";
   
   ?>

 

For some reason, that causes an infinite loop. The resource ID is #12, and $number is 3. I have $i set to 0, yet for some reason this is an infinite loop. Can someone help me find the problem?

Link to comment
https://forums.phpfreaks.com/topic/122053-solved-mysterious-infinite-loop/
Share on other sites

you have forgot to put $i++; so it increments the $i variable

 

correct code:

 

  <?php
  
  mysql_connect($dbhost, $dbuser, $dbpass);
  mysql_select_db($dbactual);
  
  $query = "SELECT * FROM logs WHERE level = 8";
  $result = mysql_query($query);
  $number = mysql_num_rows($result);
  $i = 0;
  
  echo "<table><tr>";
  echo "<td><strong>Log ID</strong></td><td><strong>User</strong></td><td><strong>User Level</strong></td><td><strong>Date</strong></td><td><strong>Time</strong></td><td><strong>Action</strong></td></tr>";
  
  while ($i < $number)
{
$id = mysql_result($result,$i,"id");
$user_id = mysql_result($result,$i,"user_id");
$level = mysql_result($result,$i,"level");
$date = mysql_result($result,$i,"date");
$time = mysql_result($result,$i,"time");
$action = mysql_result($result,$i,"action");

echo "<tr><td>$id</td><td>$user_id</td><td>$level</td><td>$date</td><td>$time</td><td>$action</td></tr>";

$i++;
}

echo "</table>";
  
  ?>

also just fyi you should look into mysql_fetch_assoc instead of using a combo of _num_rows and mysql_result.  It's cleaner and much more efficient.

 

<?php
  
  mysql_connect($dbhost, $dbuser, $dbpass);
  mysql_select_db($dbactual);
  
  $query = "SELECT * FROM logs WHERE level = 8";
  $result = mysql_query($query);
  
  echo "<table><tr>";
  echo "<td><strong>Log ID</strong></td><td><strong>User</strong></td><td><strong>User Level</strong></td><td><strong>Date</strong></td><td><strong>Time</strong></td><td><strong>Action</strong></td></tr>";
  
  while ($r = mysql_fetch_assoc($result)) {
     extract($r);

echo "<tr><td>$id</td><td>$user_id</td><td>$level</td><td>$date</td><td>$time</td><td>$action</td></tr>";
}

echo "</table>";
  
  ?>

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.