Jump to content

mysql OO not working??


jonsamwell

Recommended Posts

Hi,

 

usually i use mysqli OO to connect to a database, i have a standard class. however, the server i need to put code on doesn't support mysqli so i changed my class using mysql instead of mysqli but it doesn't work??

 

i keep getting the error: "Class 'mysql' not found"

 

Can you not use mysql in a OO way?

 

see below code

 

class db {
    private $username = 'username';
    private $password = 'password';
    private $server = 'server';
    private $database_name = 'db_name';
    private $mysql = null; //mysql connection object

    /**
     * Constructs a database connection object
     * @return none
     */
    function __construct()
    {//Default Constructor
        $this->db_connect();
    }

    /**
     * opens a connection with the database
     * @return none
     */
    function db_connect()
    {
        $this->mysql = mysql_connect($this->server, $this->username, $this->password, $this->database_name);
    }
    /**
     * close the connect to the database
     * @return none
     */
    function db_close()
    {
        $this->$mysql->close();
    }

    /**
     *  <p>executes given sql statement and returns an object</p>
     * @param string $sql
     * @return mysql_result
     */
    function getQueryResults($sql)
    {//Method to return a mysql result object

        return $this->mysql->mysql_query($sql);
    }
//blah blah blah
}

Link to comment
https://forums.phpfreaks.com/topic/148980-mysql-oo-not-working/
Share on other sites

You can use mysql in an OO way, based on your error message it's sounds like to me it problem is in the code that uses this class rather than the class itself. Maybe something like

$mydbobject = new mysql();

 

which should be fine(I don't think that mysql by it's self is reserved), but it would be a problem since the class is actually named "db".

Link to comment
https://forums.phpfreaks.com/topic/148980-mysql-oo-not-working/#findComment-782273
Share on other sites

Your db class does not work because when you converted from mysqli function calls to mysql function calls, the syntax is not an exact one for one match and you must account for the differences.

 

In the following, the 4th parameter of mysql_connect() is not the database name and you need a mysql_select_db() statement as well -

 

$this->mysql = mysql_connect($this->server, $this->username, $this->password, $this->database_name);

 

This line -

 

        $this->$mysql->close();

 

should be (and calling your link resource variable "mysql" was probably not a good choice) -

 

        mysql_close($this->mysql);

 

This line -

 

        return $this->mysql->mysql_query($sql);

 

should be -

 

        return mysql_query($sql);

Link to comment
https://forums.phpfreaks.com/topic/148980-mysql-oo-not-working/#findComment-782315
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.