When testing my try-catch block with database->Select
I was expecting the exception to be caught when trying to echo a PDOstatement but instead it tells me the error is not caught.
Catchable fatal error: Object of class PDOStatement could not be converted to string
I don't understand what I'm doing wrong here.
Here is the code:
class database{
private $conn;
private $sqlError;
public function __construct() {
$server = "localhost";
$username = "user";
$pass = "password";
$database = 'dbname';
$dsn = sprintf('mysql:dbname=%s;host=%s', $database, $server);
try {
$this->conn = new PDO($dsn, $username, $pass);
} catch (Exception $exc) {
$this->sqlError = 'SQL Error: ' . $exc->getCode();
}
}
public function Select($query, $values=array()) {
unset($this->sqlError);
try {
$qry = $this->conn->prepare('select ' . $query);
echo $qry; //invalid operation, I expect exception to be thrown
$qry->execute($values);
$result = $qry->fetchAll(PDO::FETCH_OBJ);
$error = $qry->errorInfo();
if ($error[1]) {
$this->sqlError = 'SQL Error: ' . $error[1];
}
return $result;
} catch (Exception $exc) {
$this->sqlError = 'SQL Error: ' . $exc->getCode();
}
}
}