-1

anyone knows why i get this error:

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

for this code-snippet:

public function check_availability($dataID) {

    $query = "SELECT dump FROM dumps WHERE dump = '".$dataID."'";
    if ($stmt = $mysqli->prepare($query)) {

        $stmt->execute();

        if($stmt->rowCount() > 0) {
            return FALSE;
        } else {
            return TRUE;
        }

        $stmt->close();
    }
} 

especially for this line:

if ($stmt = $mysqli->prepare($query)) {

the variable $mysqli cant be wrong because of:

$mysqli = new mysqli($config['sql_hostname'], $config['sql_username'], $config['sql_password'], $config['sql_database']);

UPDATE:

i am calling this script in browser:

define('SECURE', true);

include "storescripts/connect_to_mysql.php";

require 'AvailabilityChecker.php';

$config = array('DB' => $mysqli,
                'Table' => 'dumps',
                'Row' => 'dump',
                'Output' => true,
                'Format' => 'JSON');

$availabilityChecker = new AvailabilityChecker($config);

if($availabilityChecker->check_availability($_POST['dataID'])) {
    echo "ok";
} else {
    echo "not ok";
} 

$mysqli is defined in "connect_to_mysql.php" so?

user2999787
  • 617
  • 1
  • 5
  • 11

1 Answers1

0

you should create an Object of mysqli and check the result

$mysqli = new mysqli("localhost", "username", "password", "dbname");
if(!$mysqli)
{
   die("can not access the db");
}

then you can use the function

$mysqli->prepare($query)
Patato
  • 1,464
  • 10
  • 12