Reputation: 21
My code is as follows:
class Database
{
private $db_host;
private $db_user;
private $db_pass;
private $db_name;
private $con;
public function __construct() {
$this->db_host = "localhost";
$this->db_user = "admin";
$this->db_pass = 'password';
$this->db_name = 'test';
$this->con = '';
}
public function connect() {
$db_name = "test";
$this->con = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
}
public function select(){
$q = "SELECT name, city FROM customers;";
mysql_select_db($this->db_name, $this->con);
$result = mysql_query($q);
return mysql_fetch_assoc($result);
}
}
$db = new Database();
$db->connect();
$tempArray = Array();
$rs = $db->select('customers', 'name, suburb');
foreach ($rs as $row)
{
echo $rs['name'] . "<br>";
}
And my table's data is
name | city
--------------
Anne | Sydney
Jane | London
The actual output is:
Anne
Anne
The desired output is:
Anne
Jane
Can someone tell me what I am doing wrong. It seems like I have missed something basic. I have read over 50 articles and nothing seems to explain what I am doing wrong.
Note: This is a scaled down version of my code. I intend to use this to make a more general object that pulls information from my database.
Thanks,
Brett
Upvotes: 1
Views: 114
Reputation: 21191
Seems kind of odd that no one has picked up on a bit of a gaff in the code here.
You define select()
like so:
public function select(){
$q = "SELECT name, city FROM customers;";
mysql_select_db($this->db_name, $this->con);
$result = mysql_query($q);
return mysql_fetch_assoc($result);
}
But, then you call it like this:
$rs = $db->select('customers', 'name, suburb');
I assume your intention was to be able to specify the table and the fields to select from the database. If it was, your select
function should look more like this:
public function select($table, $fields){
$q = "SELECT $fields FROM $table;";
mysql_select_db($this->db_name, $this->con);
return mysql_query($q);
}
From there you would follow @BenLee's example in his answer, since you need to iterate over a resultset. Each field becomes a key in an associative array.
I wouldn't recommend actually doing string insertion like this in production code, but I think it's closer to what you intended.
HTH.
Upvotes: 0
Reputation: 157839
A sane version of your class. It lacks vital parts of course, to be used in the real life, but just out of your sketch:
class Database
{
private $con;
public function __construct() {
$this->con = mysql_connect("localhost", "admin", 'password');
mysql_select_db("test",$this->con);
}
public function query($sql,$this->con){
return mysql_query($sql);
}
public function get_all($sql,$this->con){
$ret = array();
$res = mysql_query($sql);
while ($row = mysql_fetch_array($row)) {
$ret[] = $row;
}
return $ret;
}
}
$db = new Database();
$rs = $db->get_all("SELECT name, city FROM customers");
foreach ($rs as $row)
{
echo $rs['name'] . "<br>";
}
Upvotes: 0
Reputation: 53319
You need to call mysql_fetch_assoc for each row. It only returns one row of data, not the full set. For example, you could move it out into the loop:
class Database
{
/* ... */
public function select(){
$q = "SELECT name, city FROM customers;";
mysql_select_db($this->db_name, $this->con);
return mysql_query($q);
/* Remove your line here, returning the query result, not the first row */
}
}
$db = new Database();
$db->connect();
$tempArray = Array();
$result = $db->select('customers', 'name, suburb');
/* Note that I'm now using mysql_fetch_assoc to get each row from the result */
while ($row = mysql_fetch_assoc($result));
echo $row['name'] . "<br>";
}
You can use a while
loop there because after the last row has been retrieved, mysql_fetch_assoc
will return FALSE and exit the loop.
Upvotes: 1
Reputation: 100175
Should'nt it be :
foreach ($rs as $row)
{
echo $row['name'] . "<br>";
}
And:
$resArr = array();
while($res = mysql_fetch_assoc($result)) {
$resArr[] = $res;
}
return $resArr;
Upvotes: 0
Reputation: 80639
In this section of your code:
return mysql_fetch_assoc($result);
You are merely returning the first row. I suggest you to create an array.
public function select(){
$q = "SELECT name, city FROM customers;";
mysql_select_db($this->db_name, $this->con);
$result = mysql_query($q);
$toReturn = array();
while($row = mysql_fetch_assoc($result) )
$toReturn[] = $row;
return $toReturn;
}
Upvotes: 0