1

I am trying to set up a MySQL connection in a main script, and then call various functions depending on what I want to do. I am having trouble passing the connection information to the function.

I have a class "queries" which contains various functions, all which return an array. This is what the code looks like in my main script (calling function normal)

$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name) or die (mysql_error());
$stats = $queries->normal($mysqli);

And then inside of the queries resource, I have this code:

class queries {
    function normal($mysqli) {
        $query = "SELECT number, first, last FROM roster";
        $roster = $mysqli->query($query);

Then I proceed to do what I need. I cannot get this to work though. I get the error

Call to a member function normal() on a non-object

on the line that I call the function in my main file.

2
  • 1
    How is $queries created? Commented Jul 12, 2010 at 21:59
  • 2
    are you setting $queries = new queries() ? Commented Jul 12, 2010 at 22:00

5 Answers 5

5

$queries is not an object, much less a queries object. Figure out what you assigned to it instead, and assign the right thing to it :)

Ahh, I think I get it now. queries is a class, not an object. You need to make an object that is an instance of the class.

$queries_obj = new queries;
$queries_obj->normal($mysqli);
Sign up to request clarification or add additional context in comments.

8 Comments

What exactly do you mean? Are you saying that I probably already assigned a variable to $queries?
@phoffer Have you assigned anything to the variable $queries?
@phoffer It means that you should have created an instance of queries as $queries, i.e. as per the comment on your question - $queries = new queries()
@Scott Saunders - thanks, fixed it within a few seconds of the initial edit ;)
@Matchu That absolutely makes sense (after the edit), and that is exactly what the problem was.
|
2

"Call to a member function normal() on a non-object" means that you are trying to call normal() on a variable that is not an object. Probably you meant to do this:

$queries = new queries();
$stats = $queries->normal($mysqli);

Comments

1

Think of your class definition as a recipe and the object as the actual dish you made using that recipe.

As stated by others, you likely haven't instantiated (created) an object of that class.

Comments

0

I would really not recommend structuring your code like this. Passing around the mysql connection can make your function signatures incredibly cluttered. Two other options are:

  • Singleton Pattern: Encapsulate your database interaction functions into a single class, and then every time you need to call one of them, you grab an instance of that class. What's nice about this method is that you always get the same db connection, instead of opening many request. If you're not using heavy OO (lots of procedural php, or lots of global functions), this is really helpful. For example:
public function foo() {  
  $results = Db::getInstance()->query("SELECT * FROM bar");  
  // Do other stuff
  return $results;
}
  • Inheritance: This pattern is really helpful if you're working in something like MVC, or you need to have objects share functionality. Combined with a singleton, you keep a single db connection per request, and the easy access of the db instance in a member variable.
class DB {
  public function __construct() {
    $this->conn = Db::getInstance();
  }
}


class Foo extends DB {
  public function foo() {
    $this->conn->query("SELECT * FROM bar");
  }
}

Comments

0

Whilst i could agree that passing database handlers around in methods/functions isn't probably the best idea, it's not dirty to pass database objects through to classes via the constructor, especially if you have multiple connections.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.