[英]How to execute multiple mysql queries together in PHP?
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. 我想要一个选择查询和插入查询,我想使用函数(mysql_query)一起完成它们,但它不起作用。 I'm tying to do somthing like this:
我想要做这样的事情:
$sql="select * from texts; insert into date ('time') values ('2012');";
mysql_query($sql);
is there any way to do it? 有什么办法吗?
mysql_query()
sends a unique query (multiple queries are not supported) . mysql_query()
发送唯一查询(不支持多个查询) 。 That's the default behaviour.However there is a bypass for this. 这是默认的行为。但是有一个旁路。
However the result code of the first query alone will be given as output of mysql_query()
if you do this. 但是,如果执行此操作,则第一个查询的结果代码将作为
mysql_query()
输出给出。
You just have to pass flag 65536 as mysql_connect's 5th parameter . 您只需将标志65536作为mysql_connect的第5个参数传递。 the flag is defined in MySQL Client flags .
该标志在MySQL Client标志中定义。
#define CLIENT_MULTI_STATEMENTS 65536 /* Enable/disable multi-stmt support */
#define CLIENT_MULTI_RESULTS 131072 /* Enable/disable multi-results */
So edit your mysql_connect()
code to match this: 所以编辑你的
mysql_connect()
代码来匹配这个:
mysql_connect($host, $username, $password, false, 65536);
Warning: 警告:
mysql_query($query)
for the first query only in the given $query
. $query
中,您才能获得第一个查询的mysql_query($query)
结果。 You can try concatenating 131072
with 65536
for getting multiple results. 131072
与65536
连接以获得多个结果。 sql.safe_mode
is set as 1 in php.ini sql.safe_mode
设置为1,则sql.safe_mode
Another alternative will be to use mysqli
instead of mysql
library. 另一种方法是使用
mysqli
而不是mysql
库。 It supports $mysqli->multi_query()
and gives output within an array for each query. 它支持
$mysqli->multi_query()
并为每个查询提供数组内的输出。
MySQL不允许在单个语句中传递多个select查询。
mysql_query()
doesn't support multiple queries execution in normal way. mysql_query()
不支持以正常方式执行多个查询。 use something like below. 使用类似下面的东西。
<?php
$str="query1;query2;"; // say $str="select * from texts; insert into date ('time') values ('2012');";
$query = explode(';',$str);
// Run the queries
foreach($query as $index => $sql)
{
$result = mysql_query($sql);
// Perform an additional operations here
}
?>
None of the mysql api can deal with several queries simultaneously, even mysql consol utility parses your input and executes one query after another and php's mysql_query doesn't. 没有mysql api可以同时处理几个查询,甚至mysql consol实用程序解析你的输入并执行一个接一个的查询而php的mysql_query没有。 You just can write your own function doing it or just put all queries in an array and execute mysql_query for each element in a loop.
您可以编写自己的函数,或者只是将所有查询放在一个数组中,并为循环中的每个元素执行mysql_query。
You can use mysqli_multi_query function but with PHP mysqli extension. 您可以使用mysqli_multi_query函数,但使用PHP mysqli扩展。 (I recommend you to use mysqli instead of mysql extension of PHP because it includes much more features & facilities)
(我建议你使用mysqli而不是PHP的mysql扩展,因为它包含更多的功能和设施)
mysql_query()
sends a unique query mysql_query()
发送一个唯一的查询
see mysql_query 看mysql_query
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.