query(" set names 'u"/> query(" set names 'u">
Home >Database >Mysql Tutorial >mysqli multiple sql statement query
mysqli has many advantages over mysql. It is recommended that you use it. If you don’t understand it, you can check out the basic tutorial of mysql:
mysqliConnect to the database and use mysqli preprocessing prepare. Not only that, mysqli also supports multi-query features. Look at the following php code:
<?php $mysqli = new mysqli("localhost","root","","new"); $mysqli->query("set names 'utf8"); //多条sql语句 $sql = "select id,name from `user`;"; $sql .= "select id,mail from `user`"; echo $sql; if ($mysqli->multi_query($sql)){//multi_query()执行一条或多条sql语句 do{ if ($rs = $mysqli->store_result()){//store_result()方法获取第一条sql语句查询结果 while ($row=$rs->fetch_row()){ var_dump($row); echo "<br>"; } $rs->Close(); //关闭结果集 if ($mysqli->more_results()){ //判断是否还有更多结果集 echo "<hr>"; } } }while($mysqli->next_result());//next_result()方法获取下一结果集,返回bool值 } $mysqli->close(); //关闭数据库连接 ?>
Regarding some of the methods used, I have annotated very clearly. Special attention should be paid to the fact that multi_query() executes multiple Statements are separated by ;, otherwise an error will occur
The above is the detailed content of mysqli multiple sql statement query. For more information, please follow other related articles on the PHP Chinese website!