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

前端 未结 8 843
误落风尘
误落风尘 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 13:49

    You have to use MySQLi, below code works well

    <?php
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    $query  = "SELECT CURRENT_USER();";
    $query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
    
    /* execute multi query */
    if ($mysqli->multi_query($query)) {
        do {
            /* store first result set */
            if ($result = $mysqli->store_result()) {
                while ($row = $result->fetch_row()) {
                    printf("%s\n", $row[0]);
                }
                $result->free();
            }
            /* print divider */
            if ($mysqli->more_results()) {
                printf("-----------------\n");
            }
        } while ($mysqli->next_result());
    }
    
    /* close connection */
    $mysqli->close();
    ?>
    
    0 讨论(0)
  • 2020-11-22 13:49

    Yes it is possible without using MySQLi extension.

    Simply use CLIENT_MULTI_STATEMENTS in mysql_connect's 5th argument.

    Refer to the comments below Husni's post for more information.

    0 讨论(0)
  • 2020-11-22 13:52

    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);
    
    0 讨论(0)
  • 2020-11-22 13:59

    As others have answered, the mysqli API can execute multi-queries with the msyqli_multi_query() function.

    For what it's worth, PDO supports multi-query by default, and you can iterate over the multiple result sets of your multiple queries:

    $stmt = $dbh->prepare("
        select sql_calc_found_rows * from foo limit 1 ; 
        select found_rows()");
    $stmt->execute();
    do {
      while ($row = $stmt->fetch()) {
        print_r($row);
      }
    } while ($stmt->nextRowset());
    

    However, multi-query is pretty widely considered a bad idea for security reasons. If you aren't careful about how you construct your query strings, you can actually get the exact type of SQL injection vulnerability shown in the classic "Little Bobby Tables" XKCD cartoon. When using an API that restrict you to single-query, that can't happen.

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

    Update: Apparently possible by passing a flag to mysql_connect(). See Executing multiple SQL queries in one statement with PHP Nevertheless, any current reader should avoid using the mysql_-class of functions and prefer PDO.

    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.

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

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