note 76991 added to function.mysql-query
| From: | halion at gmail dot com | Date: | Thu, 09 Aug 2007 09:53:30 +0000 |
| Subject: | note 76991 added to function.mysql-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129367@lists.php.net to get a copy of this message | ||
mysql_query doesnt support multiple queries, a way round this is to use innodb and transactions
this db class/function will accept an array of arrays of querys, it will auto check every line for
affected rows in db, if one is 0 it will rollback and return false, else it will commit and return
true, the call to the function is simple and is easy to read etc
----------
class MySQLDB
{
private $connection; // The MySQL database connection
/* Class constructor */
function MySQLDB(){
/* Make connection to database */
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
}
/* Transactions functions */
function begin(){
$null = mysql_query("START TRANSACTION", $this->connection);
return mysql_query("BEGIN", $this->connection);
}
function commit(){
return mysql_query("COMMIT", $this->connection);
}
function rollback(){
return mysql_query("ROLLBACK", $this->connection);
}
function transaction($q_array){
$retval = 1;
$this->begin();
foreach($q_array as $qa){
$result = mysql_query($qa['query'], $this->connection);
if(mysql_affected_rows() == 0){ $retval = 0; }
}
if($retval == 0){
$this->rollback();
return false;
}else{
$this->commit();
return true;
}
}
};
/* Create database connection object */
$database = new MySQLDB;
// then from anywhere else simply put the transaction queries in an array or arrays like this:
function function(){
global $database;
$q = array (
array("query" => "UPDATE table WHERE something =
'something'"),
array("query" => "UPDATE table WHERE something_else =
'something_else'"),
array("query" => "DELETE FROM table WHERE something_else2 =
'something_else2'"),
);
$database->transaction($q);
}
----
Server IP: 194.145.210.3
Probable Submitter: 84.12.14.141
----
Manual Page -- http://www.php.net/manual/en/function.mysql-query.php
Edit -- https://master.php.net/note/edit/76991
Del: integrated -- https://master.php.net/note/delete/76991/integrated
Del: useless -- https://master.php.net/note/delete/76991/useless
Del: bad code -- https://master.php.net/note/delete/76991/bad+code
Del: spam -- https://master.php.net/note/delete/76991/spam
Del: non-english -- https://master.php.net/note/delete/76991/non-english
Del: in docs -- https://master.php.net/note/delete/76991/in+docs
Del: other reasons-- https://master.php.net/note/delete/76991
Reject -- https://master.php.net/note/reject/76991
Search -- https://master.php.net/manage/user-notes.php