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?

5
  • I dont think there are such type of query... but if there is let me know :) Commented May 19, 2012 at 6:58
  • 1
    @caesar What do you expect as output?. The tuples of 'texts' or the status of that insert?. !!! Commented May 19, 2012 at 7:00
  • mysql_query() doesn't support multiple queries execution. Commented May 19, 2012 at 7:00
  • 1
    you can try use INSERT ... SELECT feature of mysql. full documentation dev.mysql.com/doc/refman/5.1/en/insert-select.html Commented May 19, 2012 at 7: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. Commented May 19, 2012 at 7:14

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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.

Comments

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)

Comments

0

mysql_query() sends a unique query

see mysql_query

for multiple query you can see mysqli

Comments

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

1 Comment

oh its you.. :) ha ha, yeah that's a new info to me(65536)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.