mysql query class -> result repeats to infinity

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
markis1986
Forum Newbie
Posts: 9
Joined: Fri Oct 19, 2007 12:58 am

mysql query class -> result repeats to infinity

Post by markis1986 »

Hi,
could anyone tell me, where's my problem?

class file:

Code: Select all

class klase
{
 			function connect()
 			{
 			$this->dbconn = mysql_connect("localhost", "root");
 			}			 
 			function selectdb($db)
 			{
 			$this->sdb = mysql_select_db ($db, $this->dbconn);
 			}
 function query($query)
 {
 $this->qres = mysql_query ($query, $this->dbconn);
 $this->rez = mysql_fetch_object ($this->qres);
 }
}
I want this class to display all table's rows.
The displaying page :

Code: Select all

<?php
include("classfile.php");
$xxx = new klase;
$xxx->connect(100);
$xxx->selectdb("Mokymai");
$xxx->query("select * from 2g");
while($xxx->rez)
{
		print $xxx->rez->id ;
		print $xxx->rez->name ;		
		print $xxx->rez->type ;
		print $xxx->rez->description ;
		print "<br>" ;		
    }
?>
The result is displaying the first table row, for infinity times.
It doesn't get to the 2nd and other rows.
It works fine, when i replace

Code: Select all

while($xxx->rez)


with

Code: Select all

while($xxx->rez = mysql_fetch_object ($xxx->qres))
but i don't want to write this everytime. :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

As evidenced by your working version, rez has to be changed, whether directly by you, or otherwise.

I would recommend you create a method that changes rez for you. Depending on how you write it, it could be called in a multitude of locations.
Post Reply