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();

Edited by The Little Guy
Link to comment
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();

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.