Search code examples
javascriptphpsqlitexmlhttprequest

Object of class SQLite3Result could not be converted to string


I'm trying to get a response from an SQLite DB, using an XHR on a PHP file, but I'm unsure how to handle the SQLite Object that's being returned Currently, I'm just trying to console.log the response but I'm getting the error in the title. Do I need to stringify the response perhaps?

<?php
$database = new SQLite3('cookieOrders.sqlite');

$statement = $database->prepare('SELECT creation_time FROM orders WHERE order_id = 1;');
$result = $statement->execute();

echo $result;

?>

-

function populateOrders() {
  var x = new XMLHttpRequest();
  x.onreadystatechange=function(){
  if (x.readyState==4 && x.status==200){
    var response = x.responseText;
    console.log(response);
    }
  }
  x.open("GET","./php/queryDB.php",true);
  x.send();
  return false;
}

Solution

  • You have to fetch data. Currently you are trying to echo a SQLite3Result object, which is the result of execute() :

    $result = $statement->execute() ;
    $row = $result->fetchArray() ;
    echo json_encode($row) ;
    // or echo $row['creation_time'] ;
    // or print_r($row) ;