我一直在嘗試更新一個函數來使用mysqli而不是mysql。該功能用於生成唯一的下載密鑰。我不斷收到錯誤:致命錯誤:調用第20行非對象上的成員函數query()。 我無法弄清楚我錯了什麼,並希望有人可以給我看。謝謝。功能上的Mysqli錯誤
<?php
$dbHost = 'localhost';
$dbUsername = 'xxx';
$dbPassword = 'xxx';
$dbName = 'xxx';
//Connect with the database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
//error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
function createKey(){
//create a random key
$strKey = md5(microtime());
//check to make sure this key isnt already in use
$resCheck = "SELECT count(*) FROM downloads WHERE downloadkey = '{$strKey}' LIMIT 1";
$arrCheck = $db->query($resCheck);
if($arrCheck['count(*)']){
//key already in use
return createKey();
}else{
//key is OK
return $strKey;
}
}
//get a unique download key
$strKey = createKey();
//insert the download record into the database
$insert = $db->query("INSERT INTO downloads (downloadkey, file, expires) VALUES ('{$strKey}', 'onetimedownload.zip', '".(time()+(60*60*24*7))."')");
?>
變化'$ strKey = createKey();'到' $ strKey = createKey($ db);'並在你的函數中接收'$ db'。或者把'$ db'設爲全局。 –