Page 1 of 1

repeat fetch using class !!

Posted: Sat Nov 26, 2011 11:10 am
by amirbwb
Hello,

I want to display all data from database (repeat) and not only the last one:
In classic php I do this using:

Code: Select all

...

$row_record = mysql_fetch_assoc($record);

do
{
   echo 'Record <br />';
}while($row_record = mysql_fetch_assoc($record))
In advanced php (using class)

I decided to create my own way, so here I arrived:

Code: Select all

//class db

...

public function fetch()
	{
		$this -> result = mysql_fetch_assoc($this -> query($this -> query));
		return $this -> result;
	}
//end class

$dbh = new db;
$dbh -> query("select * from player");
$result = $dbh -> fetch();
do
{
	echo 'a';
}while($result = $dbh -> fetch())
I have created the same concept as the classic php way but it's not working ...
if I print_r($result), It will gives me an array of the data (so it work in single mode)...

Can anyone gives me an idea =) thank you

Re: repeat fetch using class !!

Posted: Sat Nov 26, 2011 11:40 am
by Celauran
Every time you call fetch(), you're running the query and returning the first result as an associative array. Have your query() function populate the result property, then use fetch() on said property. Better, create a separate class for database results and have your query() function return a new result object.

Re: repeat fetch using class !!

Posted: Sat Nov 26, 2011 3:51 pm
by amirbwb
Mmm but I am new to oop, if u may please tell me more about the separated class content ?
Thank you