0

I'm recieving the following error when trying to invoke a function:

Fatal error: Call to a member function prepare() on a non-object in /var/www/includes/functions.php on line 32

I am calling this function from another page using the following code:

if (login($email, $password, $mysql) == true) {
        // Login success 
        header('Location: ../home.php');
    } else {
        // Login failed 
        header('Location: ../error.php?error=1');
    }

Below is the code of the function:

function login($email, $password, $mysql) {
// Using prepared statements means that SQL injection is not possible. 
if ($stmt = $mysql->prepare("SELECT id, password, salt //Line 32
    FROM members
   WHERE email = ?
    LIMIT 1")) {
    $stmt->bind_param('s', $email);  // Bind "$email" to parameter.
    $stmt->execute();    // Execute the prepared query.
    $stmt->store_result();

I have looked at anwsers on other questions, from reading these I believe the $mysql variable is the problem.

I have tried adding the following code at the top of my functions.php but I am still getting the same result:

global $mysql

On my functions.php page I also have the following line of code:

include ("db_connect.php");

db_connect.php contains the following:

<?php
include_once 'db_config.php';   // Database information
$mysql = mysql_connect(HOST, USER, PASSWORD, DATABASE);
?>
Jack Brown
  • 580
  • 3
  • 6
  • 20
  • You need to create an object for the `$mysql` variable somewhere. – Yogu Mar 22 '14 at 13:56
  • possible duplicate of [Call to a member function prepare() on a non-object PHP Help](http://stackoverflow.com/questions/4463441/call-to-a-member-function-prepare-on-a-non-object-php-help) – mario Mar 22 '14 at 13:58
  • Variable names are case-sensitive. (Except when googling things. Try it out!) – mario Mar 22 '14 at 13:59

1 Answers1

3

global should be used inside the function scope, not outside/before it.

function myfunc(){ // Do NOT pass anything called $mysql to this function!
    global $mysql;
    do_stuff_with_mysql();
}

Of course, it has to be defined before declaring your function (in your db_connect.php file) :

$mysql = new PDO($dsn, $user, $pass);

See : http://fr2.php.net/manual/en/language.variables.scope.php. Alternativaly, you may want to consider object-oriented programming :

class DB {
    private $pdo;
    public function __construct() { $this->pdo = new PDO(...); }
    public function myfunction() { /* use $this->pdo here */ }
}

Important note : mysql_* functions are now deprecated. http://www.php.net/manual/en/book.pdo.php

John WH Smith
  • 2,743
  • 1
  • 21
  • 31
  • Please check my update, I have defined $mysql in an include, do I still need to use the global command? – Jack Brown Mar 22 '14 at 14:01
  • You should read about variable scopes before going any further. Defining your variable earlier doesn't make it enter all execution scopes. You need to declare it as `global` if you want it to be. Plus, you're using a $mysql parameter, which would erase this globalisation. – John WH Smith Mar 22 '14 at 14:03
  • I the start of my connections string to $mysql = new mysqli and it worked. I was previously using $mysql = mysql_connect – Jack Brown Mar 22 '14 at 14:11