function connect(){
$db = new mysqli(localhost, bludevel_PMI, password1, bludevel_PMIForm);
if ($db->connect_errno) {
echo json_encode(array($mysqli->connect_error));
return false;
exit();
}}
function disConnect(){
mysqli_close($db);
}
function downloadData(){
if ($_POST['cmd'] == "downloadData"){
$result = $db->query("SELECT * FROM Jobs");//error on this line
if($result){
while ($row = $result->fetch_array()){
$jobs[$row['PMINumber']] = $row['Address'];
}
//$result->close();
//$db->next_result();
}
$result = $db->query("SELECT * FROM Installers ORDER BY `Order` ASC");
if($result){
while ($row = $result->fetch_array()){
$installers[] = $row['Names'];
}
//$result->close();
//$db->next_result();
}
echo json_encode(array($jobs, $installers));
return true;
}}
我得到錯誤PHP Fatal error: Call to a member function query() on a non-object
當我運行這個腳本。 $ db確實是一個對象,我已經在其他 函數中成功使用它,所以問題必須在函數本身內。我是新來的PHP,所以任何 幫助非常感謝。呼叫成員函數在非對象
變量作用域。它在連接函數中定義,但不會傳遞到任何外部變量。 – mario 2013-02-23 23:44:46
[PHP:變量不能在函數內部工作嗎?]可能的重複(http://stackoverflow.com/questions/3041171/php-variable-not-working-inside-of-function) – mario 2013-02-23 23:46:16
「斷開」拼寫爲「斷開連接「,而不是」disConnect「。這不是兩個字。 – meagar 2013-02-23 23:47:49