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