List Info

Thread: Mysql query always returns a single row




Mysql query always returns a single row
country flaguser name
United States
2007-07-25 16:12:46
Hi all,

I have a really frustrating problem where my queries alway
return a
single record. I'm using a class that I wrote:

class Db {
	private $host;
	private $user;
	private $password;
	private $base;
	private $db;

	public function __construct() {
		$this->host = 'localhost';
		$this->user = 'user;
		$this->password = 'pass';
		$this->base = 'db';

		$this->connect();
	}

	public function connect() {
		$this->db = new mysqli($this->host, $this->user,
$this->password,
$this->base);

		if (mysqli_connect_errno()) {
			printf("Can't connect to MySQL Server. Errorcode:
%sn",
mysqli_connect_error());
			return false;
		}
		else {
			return true;
		}
	}

	public function query($query) {
		$result = $this->db->query($query);

		if (!$result) {
			printf("Can't execute MySQL query. Errorcode:
%sn",
mysqli_connect_error());
			return false;
		}
		else {
			$return = $result->fetch_object();
			return $return;
		}
	}
}

The thing is, the same thing occurs when I use procedural
code:

				mysql_connect('localhost','user','pass');
				mysql_select_db('db');

				$s = mysql_query('select * from page');
				$t = mysql_fetch_assoc($s);

				foreach ($t as $page) {
					echo $page['title'];
					echo $page['content'];
				}

Anyone know what I'm doing wrong?

Also, when I run the queries through phpMyadmin, they work
fine so
it's not the db.

Thanks in advance,
Conor


--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.

For information or project assistance please visit :
http://www.360psg.com

You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHPgooglegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribegooglegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---


Re: Mysql query always returns a single row
country flaguser name
South Africa
2007-07-26 00:15:16
Hi Conor,
Your problem is that you're retrieving the first row and
never retrieving
the next ones. I've changed your procedural code - you
should be able to
change the class using that... see below:

mysql_connect('localhost','user','pass');
mysql_select_db('db');

$s = mysql_query('select * from page');

While ($t = mysql_fetch_assoc($s)) {
	foreach ($t as $page) {
		echo $page['title'];
		echo $page['content'];
	}
}

See, you have to retrieve the associate array for each row -
that's where
the extra while comes in. It will move the cursor in the DB
Result to the
next one each time you run mysql_fetch_assoc... so you need
not worry about
that.  See http://za.php.net/manual/en/function.mysql-fetch-assoc
.php for a
bit more info and further examples.

Sarel

-----Original Message-----
From: Professional-PHPgooglegroups.com
[mailto:Professional-PHPgooglegroups.com] On Behalf
Of conmulligan
Sent: 25 July 2007 11:13 PM
To: Professional PHP Developers
Subject: [Pro. PHP Dev.] Mysql query always returns a single
row


Hi all,

I have a really frustrating problem where my queries alway
return a
single record. I'm using a class that I wrote:

class Db {
	private $host;
	private $user;
	private $password;
	private $base;
	private $db;

	public function __construct() {
		$this->host = 'localhost';
		$this->user = 'user;
		$this->password = 'pass';
		$this->base = 'db';

		$this->connect();
	}

	public function connect() {
		$this->db = new mysqli($this->host, $this->user,
$this->password,
$this->base);

		if (mysqli_connect_errno()) {
			printf("Can't connect to MySQL Server. Errorcode:
%sn",
mysqli_connect_error());
			return false;
		}
		else {
			return true;
		}
	}

	public function query($query) {
		$result = $this->db->query($query);

		if (!$result) {
			printf("Can't execute MySQL query. Errorcode:
%sn",
mysqli_connect_error());
			return false;
		}
		else {
			$return = $result->fetch_object();
			return $return;
		}
	}
}

The thing is, the same thing occurs when I use procedural
code:

				mysql_connect('localhost','user','pass');
				mysql_select_db('db');

				$s = mysql_query('select * from page');
				$t = mysql_fetch_assoc($s);

				foreach ($t as $page) {
					echo $page['title'];
					echo $page['content'];
				}

Anyone know what I'm doing wrong?

Also, when I run the queries through phpMyadmin, they work
fine so
it's not the db.

Thanks in advance,
Conor




--~--~---------~--~----~------------~-------~--~----~
This group is managed and maintained by the development
staff at 360 PSG. An enterprise application development
company utilizing open-source technologies for todays
small-to-medium size businesses.

For information or project assistance please visit :
http://www.360psg.com

You received this message because you are subscribed to the
Google Groups "Professional PHP Developers"
group.
To post to this group, send email to Professional-PHPgooglegroups.com
To unsubscribe from this group, send email to
Professional-PHP-unsubscribegooglegroups.com
For more options, visit this group at http:
//groups.google.com/group/Professional-PHP
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )