获取错误:类mysqli_result的对象无法转换为字符串

时间:2016-03-04 02:04:12

标签: php mysqli

我的代码非常混乱,但我想在我的网站上建立一个论坛。

<?php 
    $conn = mysqli_connect("localhost", "root", "");
    if($conn->connect_error){
        die("Connection Failed");
    }
    mysqli_select_db($conn, 'forum');
    $get= "select * from forum";
    $runq = mysqli_query($conn, $get);
    while($fetch_value=mysqli_fetch_array($runq)){
        $get_usr_name=$fetch_value['username'];
        $get_body = $fetch_value['info'];
        }
        $result = $conn->query($get);
        if(is_null($get_body)){

        }
        else{
        echo "<div id='inputform1'>";
        for($row = 1; $row < $result->num_rows; $row++){

            $dbhost = "localhost";
            $dbuser = "root";
            $dbpass = "";
            $dbname = "forum";
            $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
            if(mysqli_connect_errno()){
                die("database connection failed: ") .
                    mysqli_connect_errno() .
                    " (" . mysqli_connect_errno() . ")";

            }
            $result1 = mysqli_query($connection, "select info from forum where id='$row';");
            $result = mysqli_query($connection, "select username from forum where id='$row';");
        echo "<strong class=\"after_username\">by: {$result}</strong><br><br><p id=\"bodyforum\">{$result1}<br><br></p>";
        }
        echo "</div>";

    }
    ?>

我似乎无法发布用户名和密码;它一直在说:

  

类mysqli_result的对象无法转换为字符串

我尝试过很多不同的策略&#34;但它们似乎都没有用。有人能帮助我吗?

2 个答案:

答案 0 :(得分:1)

mysqli查询的返回值是结果对象。它不仅仅是一组行;它还包含有关查询,成功或失败,错误消息和其他内容的信息。您需要显式使用result object's methods来访问查询返回的行。

答案 1 :(得分:1)

我认为问题来自这一行

{ id: "myid", event source: "eventsource" } 

echo "<strong class=\"after_username\">by: {$result}</strong><br><br><p id=\"bodyforum\">{$result1}<br><br></p>"; $result不是字符串。您需要将其循环然后访问记录。