Diego Barreiro Diego Barreiro - 2 months ago 5
PHP Question

Catchable fatal error: Object of class PDOStatement could not be converted to string (with fetchAll function)


Hi

I'm getting this error while trying to execute a php arrangement:


Catchable fatal error: Object of class PDOStatement could not be converted to string in /home/u722941258/public_html/cuenta/account/chat/index.php on line 56





The code of the file:

<?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>
";
} ?>





So, how can I fix it?

Thanks

Answer

This code is all being added to a string almost definitely incorrectly, note the . after

<?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>
  ";
} ?>

I assume you ment something like this:

<?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>";
} 
?>
Comments