Jump to content

mysql_query in OOP. Im new to this.


acefirefighter

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.