0

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.

1 Answer 1

1

Why are you still using mysql_? It's deprecated.

Anyway, use mysql_fetch_assoc instead of mysql_fetch_row

Sign up to request clarification or add additional context in comments.

2 Comments

It was first I found online. What should I use then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.