Jump to content

PDO multiple queries


The Little Guy

Recommended Posts

I am learning PDO with MySQL, and was wondering how can I do 2 queries?

 

<?php
$sql = $pdo->prepare("select sql_calc_found_rows ...");
$pdo->execute();

 

With that how can I run select found_rows()?

 

Is it safe to do this?

<?php
$sql = $pdo->prepare("select sql_calc_found_rows ...");
$pdo->execute();
$numrows = $pdo->query("select found_rows()")->fetchColumn();

Link to comment
https://forums.phpfreaks.com/topic/273078-pdo-multiple-queries/
Share on other sites

Lazy.

 

http://us3.php.net/PDO

http://us3.php.net/manual/en/pdostatement.rowcount.php

 

If you're doing a select, yes do two queries. I don't even understand why there's a question of IF you can do two queries. 

 

The question doesn't contain an "IF" it's contains a "How". Sorry if I am being confusing.

 

Here is the full query I am asking about, this is how I am assuming it is done, correct me if I am wrong:

 

<?php
$sql = $pdo->prepare("select sql_calc_found_rows question, question_id, count(a.answer_id) total,
            match(question) against (:q in boolean mode) as score
            from questions q
            left join answers a using(question_id)
            where
            match(question) against (:q in boolean mode)
            and q.deleted < 4 and (a.deleted < 4 or a.deleted is null)
            group by q.question_id order by score desc limit $limit, $max");
$sql->bindParam(":q", $_GET["q"], PDO::PARAM_STR);
$sql->execute();
$numrows = $pdo->query("select found_rows()")->fetchColumn();

You have to fetch all the results from your first query before you can run your second query, otherwise you'll get an error regarding commands out of sync.

 

eg:

$sql = "select ...";
$stmt = $db->prepare($sql);
$stmt->exec();
$results = $stmt->fetchAll();

$sql = "select found_rows()";
$stmt = $db->query($sql);
$rows = $stmt->fetchColumn(0);
$stmt->closeCursor();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.