acefirefighter Posted July 29, 2010 Share Posted July 29, 2010 I am getting a "mysqli_query() expects parameter 1 to be mysqli, null given" error. the code I am using is below but simplified. connect() does connect to the database without throwing any errors but when I get down to query() I get the above error. public function connect() { mysqli_connect(self::DBHOST, self::DBUSER, self::DBPASS, self::DBNAME); } public function query($query) { mysqli_query(self::connect(), $query); } below is what I am using to call the function. I am not sure if I have to pass the $link in the query function too. I have tried but it didn't seem to do any better. $link = db::connect(); $query = "SELECT name FROM plugins"; db::query($query); I am sure this is something simple that I have overlooked but I am new to this and still learning. Thank you for any help you can provide. Link to comment https://forums.phpfreaks.com/topic/209253-mysql_query-in-oop-im-new-to-this/ Share on other sites More sharing options...
Mchl Posted July 29, 2010 Share Posted July 29, 2010 You should take a look at implementing a singleton pattern for this class. As it is now it doesn't make much sense. Link to comment https://forums.phpfreaks.com/topic/209253-mysql_query-in-oop-im-new-to-this/#findComment-1092690 Share on other sites More sharing options...
acefirefighter Posted July 29, 2010 Author Share Posted July 29, 2010 Thanks for the fast reply. Looked into the singleton pattern and I think I have done pretty well sticking to that but I am new so I can't always be sure. I gave snippets of my code and that may have been my mistake. Ill include it all in this post. <?php class db extends config { // config defines the login info for the database // open a database connection public function connect() { // all of the connection parameters are defined elsewhere and I can connect without errors. mysqli_connect(self::DBHOST, self::DBUSER, self::DBPASS, self::DBNAME); }// query the database public function query($query) {// this is where the error is being thrown mysqli_query(self::connect(), $query); } } $query = "SELECT name FROM user"; db::query($query); Link to comment https://forums.phpfreaks.com/topic/209253-mysql_query-in-oop-im-new-to-this/#findComment-1092745 Share on other sites More sharing options...
Mchl Posted July 29, 2010 Share Posted July 29, 2010 Mmm... not really... this code is not even valid... How about like this: class dbConnection { private static $mysql; public static get() { if(!isset(self::$mysql)) { //this is a bit ugly - an implicit dependency on config class self::$mysql = new mysqli(config::DBHOST, config::DBUSER, config::DBPASS, config::DBNAME); } return self::$mysql; } } And you use it like this: $mysql = dbConnection::get(); $query = "SELECT name FROM user"; $result = $mysql->query($query); ... Link to comment https://forums.phpfreaks.com/topic/209253-mysql_query-in-oop-im-new-to-this/#findComment-1092804 Share on other sites More sharing options...
acefirefighter Posted July 29, 2010 Author Share Posted July 29, 2010 OK. I have spent an hour or so looking over the code you sent me. I think it's so simple it didn't quite make sense at first. I do get it now though. As you said a dependency on the config class isn't the best idea. What would you suggest? Setting the connection credentials in the dbConnection class??? Link to comment https://forums.phpfreaks.com/topic/209253-mysql_query-in-oop-im-new-to-this/#findComment-1092840 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.