repeat fetch using class !!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
amirbwb
Forum Commoner
Posts: 89
Joined: Sat Oct 30, 2010 6:10 pm

repeat fetch using class !!

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: repeat fetch using class !!

Post 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.
User avatar
amirbwb
Forum Commoner
Posts: 89
Joined: Sat Oct 30, 2010 6:10 pm

Re: repeat fetch using class !!

Post by amirbwb »

Mmm but I am new to oop, if u may please tell me more about the separated class content ?
Thank you
Post Reply