3

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);

is there any way to do it?

Botz3000
  • 39,020
  • 8
  • 103
  • 127
caesar
  • 129
  • 2
  • 3
  • 13
  • I dont think there are such type of query... but if there is let me know :) – sujal May 19 '12 at 06:58
  • 1
    @caesar What do you expect as output?. The tuples of 'texts' or the status of that insert?. !!! – NT_ May 19 '12 at 07:00
  • `mysql_query()` doesn't support multiple queries execution. – arun May 19 '12 at 07:00
  • 1
    you can try use INSERT ... SELECT feature of mysql. full documentation http://dev.mysql.com/doc/refman/5.1/en/insert-select.html – channa ly May 19 '12 at 07:01
  • 1
    You can execute 2 queries, since they don't seem related... you can also create a PROCEDURE that executes that insert and also that select. – porfiriopartida May 19 '12 at 07:14

6 Answers6

5

mysql_query() sends a unique query (multiple queries are not supported) . 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.

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 */

So edit your mysql_connect() code to match this:

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. 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

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.

NT_
  • 2,216
  • 1
  • 18
  • 23
1

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

Sachin Puri
  • 876
  • 7
  • 6
0

mysql_query() doesn't support multiple queries execution in normal way. 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
}
?>
arun
  • 3,667
  • 3
  • 29
  • 54
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.

nifuki
  • 31
  • 1
  • 1
  • 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)

Ehsan Khodarahmi
  • 4,772
  • 10
  • 60
  • 87
0

mysql_query() sends a unique query

see mysql_query

for multiple query you can see mysqli

Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51