jonsamwell Posted March 11, 2009 Share Posted March 11, 2009 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 More sharing options...
PFMaBiSmAd Posted March 11, 2009 Share Posted March 11, 2009 The name of your class is db, not mysql - class db { If that is not the problem post the full error message and the code where the error is being generated. Link to comment https://forums.phpfreaks.com/topic/148980-mysql-oo-not-working/#findComment-782271 Share on other sites More sharing options...
DocSeuss Posted March 11, 2009 Share Posted March 11, 2009 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 More sharing options...
Mchl Posted March 11, 2009 Share Posted March 11, 2009 mysql extension doesn't have OO interface. Link to comment https://forums.phpfreaks.com/topic/148980-mysql-oo-not-working/#findComment-782274 Share on other sites More sharing options...
jonsamwell Posted March 11, 2009 Author Share Posted March 11, 2009 it's not the way the class is called i think as Mchl said "mysql extension doesn't have OO interface." never mind Regards Jon Link to comment https://forums.phpfreaks.com/topic/148980-mysql-oo-not-working/#findComment-782279 Share on other sites More sharing options...
PFMaBiSmAd Posted March 11, 2009 Share Posted March 11, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.