有誰知道爲什麼我得到這個錯誤:準備功能用錯了?
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']);
UPDATE:
我打電話這個腳本的瀏覽器:
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」 的定義是這樣的嗎?
顯然,'$ mysqli'是*錯*。 – Vallentin
你在哪裏定義'範圍內的$ mysqli'(http://php.net/manual/en/language.variables.scope.php)? – PeeHaa
檢查此問題http://stackoverflow.com/questions/4463441/call-to-a-member-function-prepare-on-a-non-object-php-help – vijaykumar