title: PHP Fatal error: Call to a member function prepare() on a non-object
body: <p>I'm recieving the following error when trying to invoke a function:</p>
  
  <pre><code>Fatal error: Call to a member function prepare() on a non-object in /var/www/includes/functions.php on line 32
  </code></pre>
  
  <p>I am calling this function from another page using the following code:</p>
  
  <pre><code>if (login($email, $password, $mysql) == true) {
          // Login success 
          header('Location: ../home.php');
      } else {
          // Login failed 
          header('Location: ../error.php?error=1');
      }
  </code></pre>
  
  <p>Below is the code of the function:</p>
  
  <pre><code>function login($email, $password, $mysql) {
  // Using prepared statements means that SQL injection is not possible. 
  if ($stmt = $mysql-&gt;prepare("SELECT id, password, salt //Line 32
      FROM members
     WHERE email = ?
      LIMIT 1")) {
      $stmt-&gt;bind_param('s', $email);  // Bind "$email" to parameter.
      $stmt-&gt;execute();    // Execute the prepared query.
      $stmt-&gt;store_result();
  </code></pre>
  
  <p>I have looked at anwsers on other questions, from reading these I believe the $mysql variable is the problem.</p>
  
  <p>I have tried adding the following code at the top of my functions.php but I am still getting the same result:</p>
  
  <pre><code>global $mysql
  </code></pre>
  
  <p>On my functions.php page I also have the following line of code:</p>
  
  <pre><code>include ("db_connect.php");
  </code></pre>
  
  <p>db_connect.php contains the following:</p>
  
  <pre><code>&lt;?php
  include_once 'db_config.php';   // Database information
  $mysql = mysql_connect(HOST, USER, PASSWORD, DATABASE);
  ?&gt;
  </code></pre>