如何在PHP中一起执行多个mysql查询?

时间:2022-07-22 01:05:02

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:

我想要一个select查询和一个insert查询,我想使用function(mysql_query)一起执行它们,但它不起作用。我想做一些像这样的事情:

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

is there any way to do it?

有什么办法吗?

6 个解决方案

#1


5  

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.

但是,第一个查询的结果代码将作为mysql_query()的输出作为输出,如果您这样做的话。

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

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

#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:

警告:

  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. 只有在给定的$查询中,您才会得到第一个查询的mysql_query($query)的结果。您可以尝试将131072与65536连接起来以获得多个结果。
  3. This will not work on PHP < 4.3.0
  4. 这对PHP < 4.3.0不起作用
  5. This will not work if sql.safe_mode is set as 1 in php.ini
  6. 如果是sql,这将不起作用。php.ini中将safe_mode设置为1

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.

另一种选择是使用mysqli而不是mysql库。它支持$ sqmyli ->multi_query()并为每个查询在数组中提供输出。

#2


1  

MySQL don't allow passing more than one select query in single statement.

MySQL不允许在一条语句中传递多个select查询。

#3


0  

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. 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 api中没有一个可以同时处理多个查询,甚至mysql consol实用程序也会解析您的输入并执行一个又一个查询,而php的mysql_query则不会。您只需要编写自己的函数,或者将所有查询放在一个数组中,并为循环中的每个元素执行mysql_query。

#4


0  

You can use mysqli_multi_query function but with PHP mysqli extension. (I recommend you to use mysqli instead of mysql extension of PHP because it includes much more features & facilities)

可以使用sqmyli_multi_query函数,但是可以使用PHP mysqli扩展。(我建议您使用mysqli而不是PHP的mysql扩展,因为它包含更多的特性和功能)

#5


0  

mysql_query() sends a unique query

mysql_query()发送一个惟一的查询

see mysql_query

看到mysql_query

for multiple query you can see mysqli

对于多个查询,可以看到mysqli

#6


0  

mysql_query() doesn't support multiple queries execution in normal way. use something like below.

mysql_query()不支持以正常方式执行多个查询。使用下面的类似。

<?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
}
?>

#1


5  

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.

但是,第一个查询的结果代码将作为mysql_query()的输出作为输出,如果您这样做的话。

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

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

#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:

警告:

  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. 只有在给定的$查询中,您才会得到第一个查询的mysql_query($query)的结果。您可以尝试将131072与65536连接起来以获得多个结果。
  3. This will not work on PHP < 4.3.0
  4. 这对PHP < 4.3.0不起作用
  5. This will not work if sql.safe_mode is set as 1 in php.ini
  6. 如果是sql,这将不起作用。php.ini中将safe_mode设置为1

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.

另一种选择是使用mysqli而不是mysql库。它支持$ sqmyli ->multi_query()并为每个查询在数组中提供输出。

#2


1  

MySQL don't allow passing more than one select query in single statement.

MySQL不允许在一条语句中传递多个select查询。

#3


0  

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. 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 api中没有一个可以同时处理多个查询,甚至mysql consol实用程序也会解析您的输入并执行一个又一个查询,而php的mysql_query则不会。您只需要编写自己的函数,或者将所有查询放在一个数组中,并为循环中的每个元素执行mysql_query。

#4


0  

You can use mysqli_multi_query function but with PHP mysqli extension. (I recommend you to use mysqli instead of mysql extension of PHP because it includes much more features & facilities)

可以使用sqmyli_multi_query函数,但是可以使用PHP mysqli扩展。(我建议您使用mysqli而不是PHP的mysql扩展,因为它包含更多的特性和功能)

#5


0  

mysql_query() sends a unique query

mysql_query()发送一个惟一的查询

see mysql_query

看到mysql_query

for multiple query you can see mysqli

对于多个查询,可以看到mysqli

#6


0  

mysql_query() doesn't support multiple queries execution in normal way. use something like below.

mysql_query()不支持以正常方式执行多个查询。使用下面的类似。

<?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
}
?>