doumigu9594
2018-10-14 14:06
浏览 404
已采纳

PHP / MYSQL错误:类mysqli_result的对象无法转换为字符串

I've just started learning about mySQL together with PHP(basics), and I am stuck for 2 days now with this error. I have HTML code :

<html>
<head>
    <title>Title</title>
</head>
<body>
    <?php include 'pehape.php' ?>
<span>Username:</span><?php echo $var ?>
</body>
</html>

I am just trying to get the name of an user from database using this code:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";



$conn = new mysqli($servername, $username, $password, $dbname);

$var = $conn->query("SELECT Name FROM users WHERE ID=0");

?>

This is what I get as a result in web browser.

Username:
Recoverable fatal error: Object of class mysqli_result could not be converted to string in /storage/ssd3/749/7441749/public_html/index.php on line 7

Now after the result that I am getting is Object but I don't know how to convert it to string using PHP. -Does anyone know how to fix this?To convert Object to string.

I have tried using print_r function and it didn't work out for me.I found very little information about this.Also, this is my database structure if you need it for some information( I guess that might have impact, don't know) : http://prntscr.com/l5xu9n

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtpn60029 2018-10-14 14:13
    已采纳

    $var in your code is actually an object of type mysqli_result.

    This is actually a handle to a resultset, and you have to unload the results from that handle using some sort of fetch.

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "database";
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // dont normally get ID's of zero so I chnaged this to 1
    $result = $conn->query("SELECT Name FROM users WHERE ID=1");
    
    // now get the result row
    $row = $result->fetch_assoc();     // row will be an array
    
    ?>
    

    Now in your HTML you can do this

    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <?php include 'pehape.php' ?>
    <span>Username:</span><?php echo $row['Name'];?>
    </body>
    </html>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongwuli5105 2018-10-14 14:13

    You should fetch your result like this:

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "database";
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    if($result = $conn->query("SELECT Name FROM users WHERE ID=0")) {
        $var = $result->fetch_object();
        $result->close();
    }
    ?>
    

    Then in your HTML:

    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <?php include 'pehape.php' ?>
    <span>Username:</span><?php echo $var->Name ?>
    </body>
    </html>
    

    Check the docs: http://php.net/manual/pt_BR/mysqli-result.fetch-object.php

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题