Jump to content

while loop gone bad


asucrews

Recommended Posts

so i trying to so a while look put it doesn't end

 

here the code for query to database (i cut out the working functions)

<?php

include("constants.php");
      
class MySQLDB
{
   var $connection;         //The MySQL database connection
   var $num_active_users;   //Number of active users viewing site
   var $num_active_guests;  //Number of active guests viewing site
   var $num_members;        //Number of signed-up users
   /* Note: call getNumMembers() to access $num_members! */

   /* Class constructor */
   function MySQLDB(){
      /* Make connection to database */
      $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
      mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
      
      /**
       * Only query database to find out number of members
       * when getNumMembers() is called for the first time,
       * until then, default value set.
       */
      $this->num_members = -1;
      
      if(TRACK_VISITORS){
         /* Calculate number of users at site */
         $this->calcNumActiveUsers();
      
         /* Calculate number of guests at site */
         $this->calcNumActiveGuests();
      }
   }

   /**
    *  getAnimeList
    */
  function getAnimeList($animetable){
      $q = "SELECT * FROM ".$animetable." ORDER BY title ASC";
      $result = mysql_query($q, $this->connection);
      /* Error occurred, return given name by default */
      if(!$result || (mysql_numrows($result) < 1)){
         return NULL;
      }
      /* Return result array */
      $dbarray = mysql_fetch_array($result);
      return $dbarray;
   }
};

/* Create database connection */
$database = new MySQLDB;

php?>

 

the code on the page calling the function

 

<?php
while ($row = $database->getAnimeList(anime)) { 
                      echo "<tr>";
                              echo "<td>".$row['title']."</td>";
                              echo "<td>".$row['studio']."</td>";
                              echo "<td>".$row['yearmade']."</td>";
                              echo "<td>".$row['media']."</td>";
                              echo "<td>".$row['episodes']."</td>";
                              echo "<td><a href=".$row['anidblink'].">AniDB</a> <a href=".$row['annlink'].">ANN</a> <a href=".$row['aplink'].">Anime-Planet</a></td>";
                              echo "</tr>";}
php?>

 

what happening is the loop not stoping it just keep repeting the row over and over

 

i using this code listed here for the login system:

http://www.evolt.org/PHP-Login-System-with-Admin-Features?from=250&comments_per_page=50

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/128103-while-loop-gone-bad/
Share on other sites

The problem is that it's just repeating the query over and over again, you need to separate the query function and the mysql_fetch_array() functions. You can save the result of the query in another class member and then use it in the method that uses mysql_fetch_array()... that way there will be an end to the results.

Link to comment
https://forums.phpfreaks.com/topic/128103-while-loop-gone-bad/#findComment-663411
Share on other sites

sure thing, the problem is that you're calling a function that will never return false, you need to split it up so that it can.

 

  public function getAnime($animetable){
      $q = "SELECT * FROM ".$animetable." ORDER BY title ASC";
      $result = mysql_query($q, $this->connection);
      /* Error occurred, return given name by default */
      if(!$result || (mysql_numrows($result) < 1)){
         return NULL;
      }
      $this->result = $result;
   }
   
   public function fetchRow() {
      return mysql_fetch_array($this->result);
   }

 

and then call it like

 

$database->getAnime(anime); // Are you sure it shouldn't be $anime or something similar?
while ($row = $database->fetchRow()) { 
echo "<tr>";
echo "<td>".$row['title']."</td>";
echo "<td>".$row['studio']."</td>";
echo "<td>".$row['yearmade']."</td>";
echo "<td>".$row['media']."</td>";
echo "<td>".$row['episodes']."</td>";
echo "<td><a href=".$row['anidblink'].">AniDB</a> <a href=".$row['annlink'].">ANN</a> <a href=".$row['aplink'].">Anime-Planet</a></td>";
echo "</tr>";
}

 

I added php5 access modifiers because it makes me want to gag when I write code that doesn't have them.

Link to comment
https://forums.phpfreaks.com/topic/128103-while-loop-gone-bad/#findComment-663428
Share on other sites

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.