2

I want to issue multiple mysql commands with one mysql_query function. This is my code:

$query .= "INSERT INTO `users` VALUES(1,'stack','overflow');";
$query .= "INSERT INTO `posts` VALUES('other','stack','overflow');";
mysql_query($query);

If I do that I get a warning that my syntax would be incorrect. If I echo the output, copy it and execute it in phpMyAdmin it works.

Where is the error there?

2

3 Answers 3

4
$query = "INSERT INTO `users` VALUES (1,'stack','overflow'), ('other','stack','overflow');";
mysql_query($query);

PHP does not support sending more than one query at a time via mysql_query, but you can achieve your result in a single one by using the above.

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

1 Comment

That does not insert values into posts table.
2

I think you need this?? https://www.php.net/manual/en/mysqli.multi-query.php

4 Comments

Although in my experience, one query at a time is usually far simpler :)
how is the performance with executing mysql_query() for each query? I have like 100s of queries to insert per pageload
don't know about performance, you'd have to ask someone else for that; I incorrectly assumed this was a lower-scale project
I went with mysqli solution, thanks
2

according to http://www.php.net/manual/en/function.mysql-query mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

but this guy said that you just have to pass flag 65536 as mysql_connect's 5 parameter http://www.php.net/manual/en/function.mysql-query.php#91669

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.