Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Zaheer's avatar

Raw query generating General error 2014

One of my queries is generating following error : General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll().

Here is the query :

$query2 = DB::select(DB::raw("
     SELECT date,token,(select date from token b where gate='IN' and type='BIKE' and date(date) between :fd and :td and b.token=a.token) as intime,
     (select date from token c where gate='OUT' and type='BIKE' and date(date) between :fd1 and :td1 and c.token=a.token) as outtime,
     (select amount from token d where gate='OUT' and type='BIKE' and date(date) between :fd2 and :td2 and d.token=a.token) as amount
     FROM token a where type='BIKE' and date(date) between :fd3 and :td3 group by token"), 
     array('fd'=>$fd,'td'=>$td,'fd1'=>$fd,'td1'=>$td,'fd2'=>$fd,'td2'=>$td,'fd3'=>$fd,'td3'=>$td) );

I tried converting it to PDO but the error is still there. PDO query :

$q2 = "SELECT date,token,(select date from token b where gate='IN' and type='BIKE' and date(date) between '$fd' and '$td' and b.token=a.token) as intime,
      (select date from token c where gate='OUT' and type='BIKE' and date(date) between '$fd' and '$td' and c.token=a.token) as outtime,
      (select amount from token d where gate='OUT' and type='BIKE' and date(date) between '$fd' and '$td' and d.token=a.token) as amount FROM token a where type='BIKE' and date(date) between '$fd' and '$td' group by token";

$db2 = DB::connection('mysql');
$pdo2 = $db2->getPdo();
$pdo2->setAttribute( \PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
$stmt2 = $pdo2->query( $q2 );
$query2 = $stmt2->fetchAll();

Can someone suggest What is wrong with the query. Or can help convert it to Eloquent without using raw.

0 likes
2 replies
Zaheer's avatar

Thanks, I have already gone through this post. My solution was to collect data into arrays and then process to get desired result.

Please or to participate in this conversation.