0

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

3
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by cmps
0

Usually the procedural way:

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

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

0

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.

0
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)))
0

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.

0

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

0

try this,

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

Edited by ExpertsGuide: n/a

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.