Mysql Errror 'Catchable fatal error: Object of class PDO...'

Hi guys,
banging head with the error

''Catchable fatal error: Object of class PDOStatement could not be converted to string in /home/annoorsc/public_html/alumni/content/search/search.php on line 14"

$name=$_GET['name'];

$tmp="0";
$query = $db->prepare("SELECT *  
                   FROM alumni 
               WHERE id=:tmp");
 
$params = array('tmp' => $tmp); 
 
if($name!=""){ 
  $query .= "AND name=:name "; 
  $params['name']=$name; 
} 
 

$query = $db->prepare($query); 
$query->execute($params) ; 
$total = $query->fetchColumn(); 
if($total<1){ 
  echo "<div class='alert alert-danger'>No records found.</div>"; 
  exit; 
}else{
  echo "found " . $total;
}

$query is an object, not a string, so you can’t do string concatenation with it.

2 Likes

Maybe:-

$name=$_GET['name'];

$tmp="0";
$query = "SELECT * FROM alumni  WHERE id=:tmp" ;
 
$params = array('tmp' => $tmp); 
 
if($name!=""){ 
  $query .= "AND name=:name "; 
  $params['name']=$name; 
} 
 

$query = $db->prepare($query); 
$query->execute($params) ; 
$total = $query->fetchColumn(); 
if($total<1){ 
  echo "<div class='alert alert-danger'>No records found.</div>"; 
  exit; 
}else{
  echo "found " . $total;
}
1 Like

if your queries (and conditions) get complex, consider using a query builder (such as DBAL).

1 Like

Hi Sam,

It worked!!!
but i had to create one space after the below code to get rid of "invalid parameter number "

WHERE id=:tmp

Thank you Sam, Thank you everyone.

I did not check all the code thoroughly. I just edited the part where you define the $query variable, to just be a string, not an object as @Dormilich pointed out .

1 Like

Good to know about this buddy, Thank you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.