繁体   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