PHP-无法将类mysqli_stmt的对象转换为字符串

时间:2018-07-26 07:23:50

标签: php mysql sql database

这是我获取评论和回复的脚本。它不是嵌套的,因为我不在乎它,我只想能够以与youtube类似的方式显示评论。

    <?php
 header("Access-Control-Allow-Origin: *");
ob_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Importing DBConfig.php file.
include 'DBConfig.php';

// Creating connection.
 $con = mysqli_connect($HostName,$HostUser2,$HostPass2,$DatabaseName2);

 // Creating SQL query
$query = "SELECT * FROM comments WHERE comments.id = 22 ";

$queryReplies = $con -> prepare('SELECT * FROM replies WHERE replies.id = ? ');


if ($con->connect_error) {
    header("HTTP/1.1 500 Internal Server Error");
}
else {
    if ($result = $con->query($query)) {
        header('Content-type: application/json; charset=utf-8');
        if( $result->num_rows > 0) {

        $post_arr = array();

         while($row = $result->fetch_array(MYSQLI_ASSOC)) // First loop for comment
         {
           $commentID = $row['uniqueID'];
           extract($row);

           $post_item = array(
               'uniqueID' => $uniqueID,
            'comment' => $comment,
                    'date' => $date,
                    'user' => $user
        );

        $queryReplies->bind_param("s",$commentID);
        $resultReplies = $con->$queryReplies->execute();

           while($rowReplies = $resultReplies->fetch_array(MYSQLI_ASSOC)) // Second loop for replies to comment
           {
                extract($rowReplies);

                $reply_array = array(
                    'comment' => $comment,
                    'date' => $date,
                    'user' => $user
                );
                array_push($post_item,$reply_array);

           }

           array_push($post_arr,$post_item);

         }
         echo json_encode($post_arr);

        }
    }
}

 mysqli_close($con);

 ob_end_flush();
?>

所以基本上,我希望它返回块。我的数据库中有一个评论和回复表。

它给了我以下错误,我不知道如何解决。错误是什么意思,如果有的话我该如何解决?我被卡住了。

<br />
<b>Recoverable fatal error</b>:  Object of class mysqli_stmt could not be converted to string in <b>C:\Apache24\htdocs\getComments.php</b> on line <b>45</b><br />

经过牛头怪的建议并从$ querylines中删除了-> con,现在它说

    <br />
 <b>Fatal error</b>:  Uncaught Error: Call to a member function fetch_array() on boolean in C:\Apache24\htdocs\getComments.php:47
Stack trace:
#0 {main}
  thrown in <b>C:\Apache24\htdocs\getComments.php</b> on line <b>47</b><br />

1 个答案:

答案 0 :(得分:2)

<input type="date" max="1979-12-31">

由于在$resultReplies = $con->$queryReplies->execute();部分中包含了$,PHP认为您想访问$con->$queryReplies对象的属性,而实际上您只想使用{{1} }。放下$con部分。

通常,您查看PHP指出正在发生错误的行,并查看该行是否存在问题。在这种情况下,您可能知道$queryReplies包含一个$con->对象。至少那是让我指出错误原因的原因:)。