在 PHP 中组合多个 MySQL 查询
在 PHP/MySQL 中,不可能将多个查询作为单行代码执行常规 mysql API。然而,还有其他方法可以达到相同的结果。
使用 MySQLi 的 mysqli_multi_query 函数
MySQLi 提供了一个名为 mysqli_multi_query 的函数,允许您一次性执行多个查询。语法:
mysqli_multi_query(mysqli $link, string $query);
例如:
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name'); $query = "SELECT SQL_CALC_FOUND_ROWS Id, Name FROM my_table WHERE Name LIKE '%prashant%' LIMIT 0, 10;" . "SELECT FOUND_ROWS();"; $mysqli->multi_query($query); // Handle each result set separately if ($mysqli->more_results()) { do { $result = $mysqli->store_result(); // Process the current result set while($row = $result->fetch_array()) { // Do something with the row } $result->free(); } while ($mysqli->more_results() && $mysqli->next_result()); }
注意: mysql_query 和 mysql API 中其他函数的使用已弃用,应避免使用 MySQLi 或 PDO(PHP 数据
其他方法
以上是如何在 PHP 中执行多个 MySQL 查询?的详细内容。更多信息请关注PHP中文网其他相关文章!