如何在PHP中一起执行多个mysql查询? [英] How to execute multiple mysql queries together in PHP?

查看:105
本文介绍了如何在PHP中一起执行多个mysql查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个选择查询和一个插入查询,我想使用function(mysql_query)一起使用它们,但是它不起作用. 我想做这样的事情:

I want to a select query and an insert query that I want to do them together using function(mysql_query),but it's not working. I'm tying to do somthing like this:

$sql="select * from texts; insert into date ('time') values ('2012');";
mysql_query($sql);

有什么办法吗?

推荐答案

mysql_query()发送唯一查询(不支持多个查询). 这是默认的行为,但是有一个绕过的方法.

mysql_query() sends a unique query (multiple queries are not supported) . That's the default behaviour.However there is a bypass for this.

如果执行此操作,则仅将第一个查询的结果代码作为mysql_query()的输出给出.

However the result code of the first query alone will be given as output of mysql_query() if you do this.

您只需要传递标志65536作为mysql_connect的第5个参数.该标志是在 MySQL客户端标志中定义的.

You just have to pass flag 65536 as mysql_connect's 5th parameter . the flag is defined in MySQL Client flags.

#define CLIENT_MULTI_STATEMENTS 65536 /* Enable/disable multi-stmt support */
#define CLIENT_MULTI_RESULTS 131072 /* Enable/disable multi-results */

因此,请编辑您的mysql_connect()代码以使其与此匹配:

So edit your mysql_connect() code to match this:

mysql_connect($host, $username, $password, false, 65536);

警告:

  1. 对于给定的$query中的第一个查询,仅,您将获得mysql_query($query)的结果.您可以尝试将13107265536串联以获取多个结果.
  2. 这不适用于PHP< 4.3.0
  3. 如果在 php.ini
  4. 中将sql.safe_mode设置为1,这将不起作用
  1. You will get the result of mysql_query($query) for the first query only in the given $query . You can try concatenating 131072 with 65536 for getting multiple results.
  2. This will not work on PHP < 4.3.0
  3. This will not work if sql.safe_mode is set as 1 in php.ini


另一种替代方法是使用mysqli而不是mysql库.它支持$mysqli->multi_query()并在每个查询的数组内提供输出.


Another alternative will be to use mysqli instead of mysql library. It supports $mysqli->multi_query() and gives output within an array for each query.

这篇关于如何在PHP中一起执行多个mysql查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆