views:

2466

answers:

5

I have two queries, as following:

SELECT SQL_CALC_FOUND_ROWS Id, Name FROM my_table WHERE Name LIKE '%prashant%' LIMIT 0, 10;
SELECT FOUND_ROWS();

I want to execute both these queries in a single attempt.

$result = mysql_query($query);

But then tell me how I will handle each tables set separately. Actually in ASP.NET we uses dataset which handles two queries as

ds.Tables[0];
ds.Tables[1]; .. etc

How can I do the same using PHP/MYSQL.

Thanks

+1  A: 

Like this:

$result1 = mysql_query($query1);
$result2 = mysql_query($query2);

// do something with the 2 result sets...

if ($result1)
    mysql_free_result($result1);

if ($result2)
    mysql_free_result($result2);
Jon Benedicto
Didn't he want to avoid executing mysql_query twice?
Elazar Leibovich
Yes, I want to avoid mysql querries twice. And in @Jon Benedicto answer, he's executing two queries. :(
Prashant
I've just posted another answer that shows how to do execute both queries in one go using MySQLi.
Jon Benedicto
A: 

It says on the PHP site that multiple queries are NOT permitted (EDIT: This is only true for the mysql extension. mysqli and PDO allow multiple queries) . So you can't do it in PHP, BUT, why can't you just execute that query in another mysql_query call, (like Jon's example)? It should still give you the correct result if you use the same connection. Also, mysql_num_rows may help also.

Michael Pryor
This is only true for the mysql extension. mysqli and PDO allow multiple queries.
Ionuț G. Stan
+1  A: 

Using SQL_CALC_FOUND_ROWS you can't.

The row count available through FOUND_ROWS() is transient and not intended to be available past the statement following the SELECT SQL_CALC_FOUND_ROWS statement.

As someone noted in your earlier question, using SQL_CALC_FOUND_ROWS is frequently slower than just getting a count.

Perhaps you'd be best off doing this as as subquery:

SELECT 
 (select count(*) from my_table WHERE Name LIKE '%prashant%') 
as total_rows,
Id, Name FROM my_table WHERE Name LIKE '%prashant%' LIMIT 0, 10;
tpdi
SQL_CALC_FOUND_ROWS is slower, but at http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows link I found "However, this is faster than running the query again without LIMIT, because the result set need not be sent to the client. " ??
Prashant
But not in this case, since the query can't use indices. SQL_CALC_FOUND_ROWS will result in a single table scan, while your subquery will result in two, and therefore is much slower. Also, You've forgotten a comma after total_rows.
Emil H
Yes, well, you're not running the query again without the limit. You're running a count(*), which sends one row. Really, Emil H has a good point -- it's a micro optimization of dubious value. Either run two queries, or if you absolutely must do this, do it as a subquery as I've explained. (Emil H, thanks for pointing out teh missing comma.)
tpdi
You're welcome. :)
Emil H
+6  A: 

You can't do that using the regular mysql-api in PHP. Just execute two queries. The second one will be so fast that it won't matter. This is a typical example of micro optimization. Don't worry about it.

For the record, it can be done using mysqli and the mysqli_multi_query-function.

Emil H
I am basically concerned about the performance, which I think will decrease if we'll hit the database two times.
Prashant
Do a benchmark if you're not convinced. The time it will take to execute the SELECT FOUND_ROWS() query and retrieve the result is negligable. You already have the connection, and the result set is extremely small (just one integer).
Emil H
BTW, If you're worried about performance you should be spending your time trying to get rid of the LIKE '%prashant%' statement. That's possibly a real performance issue. As a rule of thumb: The bottle neck for most php applications is not in php at all, but rather in the database. If you want to spend time optimizing your application, spend it reviewing your indices and EXPLAIN:ing your queries.
Emil H
Ok, thanks @Emil :)
Prashant
+1  A: 

You'll have to use the MySQLi extension if you don't want to execute a query twice:

if (mysqli_multi_query($link, $query))
{
    $result1 = mysqli_store_result($link);
    $result2 = null;

    if (mysqli_more_results($link))
    {
        mysqli_next_result($link);
        $result2 = mysqli_store_result($link);
    }

    // do something with both result sets.

    if ($result1)
        mysqli_free_result($result1);

    if ($result2)
        mysqli_free_result($result2);
}
Jon Benedicto