separate statements in single MYSQL query
How do I separate statements in a single MYSQL query?
I开发者_如何学Pythons it a semicolon?
I don't think it matters but I am writing the statements to be invoked by PHP functions
Edit: the problem is that I didn't seem to understand the UPDATE
syntax, that you can execute multiple fields with one UPDATE
statement
Using the mysqli_ functions (docs), you can pass multiple queries in the same string using mysqli_multi_query
(docs)
Example from the docs:
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();";
$query .= 'UPDATE City SET name = "Boston" WHERE id = 5';
/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
/* close connection */
$mysqli->close();
?>
If you're using one PHP call to get data from or send data to the database, it won't work. The MySQL PHP drivers support execution of a query string, not a script. If you want to execute multiple commands with one call, check out stored procedures.
http://dev.mysql.com/doc/refman/5.0/en/stored-routines.html
I think you're trying to do this?
<?php
$query = "SELECT name, subject, message FROM contact;select name from contact;";
$result = mysql_query($query);
?>
It won't work because PHP doesn't know what query you are looking to execute. You can do the following though, but most likely you knew that:
<?php
$query = "SELECT name, subject, message FROM contact";
$query2 = "SELECT name, message FROM contact;select name from contact;";
$result = mysql_query($query);
$result2 = mysql_query($query2);
?>
Hope this helps,
Jeffrey Kevin Pry
精彩评论