Hello,

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

...

$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:

//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

Recommended Answers

All 8 Replies

Member Avatar for diafol

Usually the procedural way:

$r = mysql_query(...sql...);
while($data=mysql_fetch_array($r)){
  //do something with the data
}

Usually the procedural way:

$r = mysql_query(...sql...);
while($data=mysql_fetch_array($r)){
  //do something with the data
}

This won't change anything ...
I want it using the class

Member Avatar for diafol

I purposely didn't give anything on OOP as you haven't included much I can help you with.

Which DB class are you using? I'm assuming PDO, but you don't mention.

class db
{
	private $connection;
	private $preapre;
	public $query;
	public $result;
	
	public function __construct()
	{
		$this -> connection = mysql_pconnect('localhost','root','') or die(mysql_error());
		mysql_select_db('oop',$this -> connection) or die(mysql_error());
	}
	
	public function prepare($prepare)
	{
		mysql_query($prepare,$this -> connection);
	}
	
	public function query($query)
	{
		$this -> query = $query;
		return mysql_query($query,$this->connection);
	}
	
	public function fetch()
	{
		$this -> result = mysql_fetch_assoc($this -> query($this -> query));
		return $this -> result;
	}
	
}

$dbh = new db;
$dbh -> query("select * from player");
$result = $dbh -> fetch();
do
{
	echo 'a';
}while($db -> result = mysql_fetch_assoc($db -> query($db -> query)))
Member Avatar for diafol

Ok a few things:

$dbh = new db;
$dbh -> query("select * from player");
$result = $dbh -> fetch();
do
{
	echo 'a';
}while($db -> result = mysql_fetch_assoc($db -> query($db -> query)))

Is outside the class, so it has no access to the mysql_* functions.

while($db -> result = mysql_fetch_assoc($db -> query($db -> query)))

$db does not exist, but $dbh does.

I also think your fetch function is busted. If you use PDO, you should get a more robust OOP - if you don't mind using a ready made class that is.

Also your:

private $preapre;

is mis-spelled.

Good luck with it.

Sorry, I made this mistake, but it's not working :S

I tried also

do
{
	echo 'a';
}while($result = $dbh -> fetch());

Can't understand why it's not working !!

try this,

$dbh = new db;
$dbh -> query("select * from player");
$result = $dbh -> fetch();
while($row = mysql_fetch_assoc($result)
{
	print_r ($row);
}

Sorry but it doesn't work ...
Btw you cannot fetch 2 times

in your script your doing
fetch (fetch()) which is not correct ...

.........

I have done this using PDO:

$dbh = new PDO(etc..)

$query = $dbh -> query('select * from TABLE');
$result = $query -> fetch();

do{
echo 'a';
}while($result = $query -> fetch())

And it work correctly!!

but am searching to do this by myself ...
is there any way to check what the PDO class is made by ??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.