note 37870 added to function.mysql-query
| From: | PredragSupurovic at rn2 dot php dot net | Date: | Sun, 30 Nov 2003 21:52:48 +0000 |
| Subject: | note 37870 added to function.mysql-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-61174@lists.php.net to get a copy of this message | ||
If you need to execute sevaral SQL commands in a row (usually called batcg SQL) using PHP you canot
use mysql_query() since it can execute single command only.
Here is simple but effective function that can run batch SQL commands. Take cere, if string contains
semicolon (;) anywhere except as command delimiter (within string expression for example) function
will not work.
function mysql_exec_batch ($p_query, $p_transaction_safe = true) {
if ($p_transaction_safe) {
$p_query = 'START TRANSACTION;' . $p_query . '; COMMIT;';
};
$query_split = preg_split ("/[;]+/", $p_query);
foreach ($query_split as $command_line) {
$command_line = trim($command_line);
if ($command_line != '') {
$query_result = mysql_query($command_line);
if ($query_result == 0) {
break;
};
};
};
return $query_result;
}
----
Manual Page -- http://www.php.net/manual/en/function.mysql-query.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37870
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37870&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37870&report=yes
Search -- http://master.php.net/manage/user-notes.php