-2

class for MVC to return fetch_array/object results

  class DbDriver{
    private $connection;
    private $query;
    private $result;
    public function connect()
        {
        $host             = 'localhost';
        $user             = '';
        $password         = '';
        $database         = '';
        $this->connection = mysql_connect($host, $user, $password);
        $this->connection = mysql_select_db($database);
        return TRUE;
        }
    public function disconnect()
        {
        $this->connection->mysql_close();
        return TRUE;
        }
    public function prepare($query)
        {
        $this->query = $query;
        return TRUE;
        }
    public function query()
        {
        if (isset($this->query))
            {
            $this->result = $this->connection->mysql_query($this->query);
            return TRUE;
            }
        return FALSE;
        }
    public function fetch($type = 'object')
        {
        if (isset($this->result))
            {
            switch ($type)
            {
                case 'array':
                    $row = $this->result->mysql_fetch_array();
                    break;
                case 'object':
                default:
                    $row = $this->result->mysql_fetch_object();
                    break;
            }
            return $row;
            }
        return FALSE;
        }
    }

returns

[Wed May 30 11:55:42 2012] [error] [client] PHP Fatal error: Call to a member function mysql_query() on a non-object in /var/www/httpdocs/test/mysql.php on line 30

line 30

        $this->result = $this->connection->mysql_query($this->query);

ill go nuts. >.<

tereško
  • 58,060
  • 25
  • 98
  • 150
  • 4
    Interesting: You are talking about OOP and then you use the outdated `mysql`-extension ... You should use `MySQLi`, or `PDO_MySQL` instead. – KingCrunch May 30 '12 at 09:12
  • 3
    `mysql_select_db` doesn't return what you think it does. – DCoder May 30 '12 at 09:12
  • 1
    `$this->connection = mysql_connect($host, $user, $password);` and `$this->connection = mysql_select_db($database); ` why you are using same variable again ? – Sanjay May 30 '12 at 09:16
  • possible duplicate of [Call to a member function on a non-object](http://stackoverflow.com/questions/54566/call-to-a-member-function-on-a-non-object) – tereško Apr 30 '13 at 18:05

2 Answers2

5

Because mysql_query:

Returns a MySQL link identifier on success or FALSE on failure.

so $this->connection is not object.

Please use PDO driver or MySQLi, mysql_* extensions will be deprecated in the future.

Good PDO examples

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
0

Here is your problem

$this->connection = mysql_select_db($database);

mysql_select_db returns bool

But please take a look on PDO or mysqli

Milan Halada
  • 1,943
  • 18
  • 28