How to execute two mysql queries as one in PHP/MYSQL?

前端 未结 8 829
误落风尘
误落风尘 2020-11-22 13:19

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


        
相关标签:
8条回答
  • 2020-11-22 14:09

    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.

    0 讨论(0)
  • 2020-11-22 14:10

    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);
    }
    
    0 讨论(0)
提交回复
热议问题