簡體   English   中英

Mysqli_query和mysqli_fetch_assoc為空

[英]Mysqli_query and mysqli_fetch_assoc are null

對於一個學校項目,我正在創建一個虛擬的網上銀行站點,該站點在學校需要一個數據庫來存儲虛擬的帳戶信息。 每次查看帳戶時,都會出現兩個mysqli錯誤。 一個在person.class.php中的第26行上,另一個在veiwaccounts.php中的52行中,我在我的人員類中跟蹤了該錯誤的主要原因,但似乎無法以任何方式對其進行修復。 錯誤是

警告:mysqli_query()期望參數1為mysqli,在第26行的... / person.class.php中給出null
警告:mysqli_fetch_assoc()期望參數1為mysqli_result,在第52行的... / viewaccounts.php中給出null

這是來自viewaccounts.php文件的一些代碼示例:

if($currentMember)
{
    $currentMember = new Person($memberid);
    $accounts =  $currentMember->retrieve_all_accounts();

    //HERE IS WHERE THE PROBLEM IS $accounts = $currentMember->retrieve_all_accounts(); 

    //Loop through accounts
    while($account = mysqli_fetch_assoc($accounts)) {
        //Retrieve balance
        $bankaccount = new Bankaccount($account['BankAccountID']);
        $bankaccount->connection = $conn;
        $balance = mysqli_fetch_assoc($bankaccount->retrieve_current_balance());

這是個人類文件的代碼:

public function retrieve_all_accounts() {
    $accounts_query = "SELECT BankAccountID FROM BankAccount WHERE UserID = " .$this->memberid;
    $result = mysqli_query($this->connection, $accounts_query);
    return $result;
}

您正在使用$memberid變量中的成員ID實例化您的person類。 嘗試在此處傳遞$memberid的值[在其中傳遞值以實例化class person的對象的位置],然后使用var_dump調試其是否真的返回了任何值。 並放置$currentMember->connection = $conn; 實例化對象后。 您的問題很可能是在這里引起的。 您試圖在實例化之前訪問類person的公共變量連接。

更改

    //Accounts
    $currentMember->connection = $conn;

    //instantiating class here since suggested code from video fails to do it.


if($currentMember)
{
    $currentMember = new Person($memberid);
    $accounts =  $currentMember->retrieve_all_accounts();

}else
{
    echo "<p>not working</p>";
}

if(!$currentMember) {
  $currentMember = new Person($memberid);
}
$currentMember->connection = $conn;
$accounts =  $currentMember->retrieve_all_accounts();

為了可讀性,我還將其移到第一個PHP代碼塊中。

您實例化一個新的Person只有$currentMember計算結果為TRUE -如果unserialize()調用失敗$currentMember將評估為FALSE ,那就是當你需要創建一個新的對象。

同樣,您首先分配了connection屬性,當您創建新的Person時,該屬性將被銷毀,大概每次您都會這樣做,否則會看到更多警告。

您還應該重構用於測試Person是否已保存在$_SESSION的整個邏輯-首先,會話將序列化對象本身,因此您無需調用serialize() / unserialize() 相反,您應該定義一個__autoload()函數,然后將對象實例存儲在$_SESSION

我還注意到,您的開發服務器似乎已啟用short_open_tag將其關閉,這是一種不好的做法,因為在現實世界中幾乎所有地方都禁用了它。

似乎您的Database對象初始化連接時遇到問題。 可能未連接,因為該錯誤表明您未向mysli_query()提供mysqli對象。

嘗試查看Database類中的mysqli_connect_error()mysqli_connect_errno() ,以找出連接時是否出錯並從那里開始工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM