Page 1 of 1

mysql query class -> result repeats to infinity

Posted: Wed Nov 14, 2007 9:42 am
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:

Posted: Wed Nov 14, 2007 9:49 am
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.