Unbuffered requests for a single request with PDO

I want to make unbuffered queries only on some queries.

In mysql, I did this:

$req = mysql_unbuffered_query('SELECT * FROM forum_topics
ORDER BY (topic_id/topic_stick) DESC, topic_last_post DESC');
while($data = mysql_fetch_assoc($req)) {
   // display results...
}

I looked at php doc, and according to it, pdowe should continue this way to make requests without a buffer:

$pdo = new PDO("mysql:host=localhost;dbname=world", 'my_user', 'my_pass');
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

$uresult = $pdo->query("SELECT Name FROM City");

if ($uresult) {
   while ($row = $uresult->fetch(PDO::FETCH_ASSOC)) {
       echo $row['Name'] . PHP_EOL;
   }
}

But is it possible to do this without buffering only for the results of the forum_topics table, without setting unbuffered for all pdo instances?

+4
source share
3 answers

if you use a prepared statement (you can use them for easy selection) as follows:

$uresult = $pdo->prepare("SELECT Name FROM City");

then you can set the attribute to uresult:

$uresult->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

then you will execute the prepared statement:

$uresult->execute();
if ($uresult) {
   while ($row = $uresult->fetch(PDO::FETCH_ASSOC)) {
       echo $row['Name'] . PHP_EOL;
   }
}

, pdo

+2

Re, , :

SQLSTATE [IM001]: :

?

: php.net doc.

:

$sth->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

.

prepare(), .

$sth = $pdo->prepare('SELECT * FROM my_table',
array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false));

, , .

+8

, SELECT, fetchAll.

                $query = 'SELECT ...... ';
                $arr = explode(' ', $query);
                $query_type = strtolower($arr[0]);
                if ($query_type == 'select') {
                    $query_response = $query_prepare->fetchAll(PDO::FETCH_ASSOC);
                } else {
                    $query_response = '';
                }

, . , .

0

All Articles