准备功能用错了吗?

时间:2013-11-23 16:16:21

标签: php function

任何人都知道为什么我会收到此错误:

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

代码片段:

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();
    }
} 

特别是对于这一行:

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

变量$ mysqli因为:

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

更新:

我在浏览器中调用此脚本:

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在“connect_to_mysql.php”中定义了吗?

1 个答案:

答案 0 :(得分:0)

您应该创建Object mysqli并检查结果

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

然后你可以使用函数

$mysqli->prepare($query)