Reputation: 398
Im trying to make very simple class, where I can retrieve data from table.
class dbCore{
private $username;
private $password;
private $database;
private $serverAddress;
private function createConnection(){
return mysql_connect($this->serverAddress, $this->username, $this->password);
}
public function __construct($db=array()){
$this->username = $db['username'];
$this->password = $db['password'];
$this->database = $db['database'];
$this->serverAddress = $db['serverAddress'];
}
public function checkConnection(){
$connection = $this->createConnection();
if( $connection )
mysql_close($connection);
return !!$connection;
}
public function getAll($tableName){
$data=array();
$connection = $this->createConnection();
$sql = "SELECT * FROM " . $tableName;
if ($connection){
mysql_select_db($this->database);
$result=mysql_query($sql, $connection);
while ($row = mysql_fetch_row($result)) {
array_push($data,$row);
}
return $data;
}else{
return false;
}
}
}
when I call $db->getAll('mytable');
it retrieves:
Array
(
[0] => Array
(
[0] => 1
[1] => Riga
[2] => Liepaja
[3] => 2014-11-20
[4] => 2014-11-08
[5] => 4
)
[1] => Array
(
[0] => 2
[1] => Riga
[2] => Liepaja
[3] => 2014-11-19
[4] => 2014-11-09
[5] => 3
)
)
But I want it to be id => 1, city => Riga
etc. But I dont want to write it myself. I want it to take it from column names and put in key values. Is there easy way to do it? I could retrieve all field names and replace them, but it seems not very efficient.
Upvotes: 0
Views: 45
Reputation: 9227
Why are you still using mysql_? It's deprecated.
Anyway, use mysql_fetch_assoc
instead of mysql_fetch_row
Upvotes: 1