簡體   English   中英

mysqli_result類的對象無法在PHP中轉換為字符串

[英]Object of class mysqli_result could not be converted to string in PHP

我正在使用PHP創建一個簡單的Web應用程序。 當我嘗試從具有適當id用戶名數據庫中獲取username名時,出現錯誤

無法將mysqli_result類的對象轉換為string

這是我的代碼。

public function get_gebruiker_id_by_name($Gebruiker) {
    $Gebruiker = $this->real_escape_string($Gebruiker);

    $Gebruikerid = $this->query("SELECT klantid FROM klanten WHERE Gebruikersnaam = '" . $Gebruiker . "'");
    if ($Gebruikerid->num_rows > 0){
        $row = $Gebruikerid->fetch_row();
        return $row[0];
    } else
        return null;
    }
    public function get_gebruikersnaam_by_id($Gebruikersid) {
        return $this->query("SELECT NaamKlant FROM klanten WHERE klantid=" . $Gebruikersid);
    }

以及對該函數的調用:

$GebruikersID = (BestellingDB::getInstance()->get_gebruiker_id_by_name($_SESSION['Username']));
$Naamklant = (BestellingDB::getInstance()->get_gebruikersnaam_by_id($GebruikersID));

echo "Welkom" . $Naamklant;

我正在使用會話來保護username 提前致謝。

您正在獲取查詢對象並回顯它。

    <?php 
    print_r( $Naamklant); // instead  of echo
    ?>

要么

public function get_gebruikersnaam_by_id($Gebruikersid) {
    $query = $this->query("SELECT NaamKlant FROM klanten WHERE klantid=" . $Gebruikersid);
//return a string (id) here
}

問題出在這里:

$Naamklant = (BestellingDB::getInstance()->get_gebruikersnaam_by_id($GebruikersID));
<?php 
  echo "Welkom" . $Naamklant;
?>

由於$Naamklant是一個SQL結果集對象,因此您不能直接通過echo打印它的值,因此需要執行以下操作:

<?php
 $row = $Naamklant->fetch_row();
 echo "Welkom" . $row['NaamKlant'];
?>

暫無
暫無

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

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