可捕获的致命错误:PDOStatement类的对象无法转换为字符串(使用fetchAll函数)

时间:2016-10-01 21:36:09

标签: php sql

我在尝试执行php安排时收到此错误:

  

可捕获的致命错误:无法在 /home/u722941258/public_html/cuenta/account/chat/index.php 中将PDOStatement类的对象转换为字符串 56


文件的代码:

<?php
session_start();
require_once '../../access/functions.php';
$user_index = new USER();

if(!$user_index->is_logged_in())  { $user_index->redirect('../../access/index.php'); }

$stmt = $user_index->runQuery("SELECT * FROM users WHERE id=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);


$stmt = $user_index->runQuery("SELECT * FROM conversations WHERE user_id=:uid ORDER BY conversation_id ASC");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
Some HTML with no PHP
<?php foreach( $result as $row_conv ) {
  echo "
    <a href='#chat_".$row_conv['conversation_id']."' type='button' class='list-group-item' data-toggle='modal' data-target='#chat_".$row_conv['conversation_id']."'>".$row_conv['conv_name']."</a>\n

    <!-- Modal -->
    <div id='chat_".$row_conv['conversation_id']."' class='modal fade' role='dialog'>
      <div class='modal-dialog'>

        <!-- Modal content-->
        <div class='modal-content'>
          <div class='modal-header'>
            <button type='button' class='close' data-dismiss='modal'>&times;</button>
            <h4 class='modal-title'>Chat con ".$row_conv['conv_name']."</h4>
          </div>
          <div class='modal-body'> ".
          $stmt = $user_index->runQuery("SELECT * FROM chat WHERE user_id=:uid AND conversation_id=:conv_id ORDER BY message_id DESC");
          $stmt->bindParam(":uid",$_SESSION['userSession']);
          $stmt->bindParam(":conv_id",$row_conv['conversation_id']);
          $stmt->execute;
          $result_chat = $stmt->fetchAll(PDO::FETCH_ASSOC);

            foreach( $result_chat as $row_chat ) {
             print($row_chat['data']."/n");
           }

          " </div>
          <div class='modal-footer'>
            <button type='button' class='btn btn-default' data-dismiss='modal'>Cerrar</button>
          </div>
        </div>
      </div>
    </div>
  ";
} ?>


那么,我该如何解决呢?

由于

2 个答案:

答案 0 :(得分:1)

此代码几乎绝对错误地添加到字符串中,请注意

之后的.
<?php foreach( $result as $row_conv ) {
  echo "<HTML Code, bla bla bla>".
          $stmt = $user_index->runQuery("SELECT * FROM chat WHERE user_id=:uid AND conversation_id=:conv_id ORDER BY message_id DESC");
          $stmt->bindparam(":uid",$_SESSION['userSession']);
          $stmt->bindparam(":conv_id",$row_conv['conversation_id']);
          $stmt->execute;
          $result_chat = $stmt->fetchAll(PDO::FETCH_ASSOC);

            foreach( $result_chat as $row_chat ) {
             print($row_chat['data']."/n");
           }

          " 
     <HTML Code, bla bla bla>
  ";
} ?>

我假设你有这样的事情:

<?php 
foreach( $result as $row_conv ) {
    echo "<HTML Code, bla bla bla>";

    $stmt = $user_index->runQuery("SELECT * FROM chat WHERE user_id=:uid AND conversation_id=:conv_id ORDER BY message_id DESC");
    $stmt->bindparam(":uid",$_SESSION['userSession']);
    $stmt->bindparam(":conv_id",$row_conv['conversation_id']);
    $stmt->execute;
    $result_chat = $stmt->fetchAll(PDO::FETCH_ASSOC);

    foreach( $result_chat as $row_chat ) {
        print($row_chat['data']."/n");
    }

    echo "<HTML Code, bla bla bla>";
} 
?>

答案 1 :(得分:0)

如果您使用的是XAMPP,则需要确保您拥有可以处理PDO语句的版本。