/**
* Transaction Encapsulation method
* @access Public
* @param array $sqls The SQL array or statement to execute
* @return Boolean
*/
Public Function Transexecutesql ($SQLS, $vals) {
try {
$this->starttrans ();
if (Is_array ($SQLS)) {
foreach ($sqls as $k = = $sql) {
if (!isnull ($vals)) {
foreach ($vals [$k] as $valKey = = $val) {
$sql = $this->bindparam ($sql, $valKey + 1, $val);
}
}
$result = $this->db->execute ($sql);
if ($result = = = False) {//update data returns 0 if same as original
if (! $result)
$this->rollback ();
return false;
}
}
} else {
$result = $this->db->execute ($SQLS);
if (! $result) {
$this->rollback ();
return false;
}
}
$this->commit ();
return true;
} catch (\exception $e) {
$this->rollback ();
//
$sxLog = new \org\log\sxlog ();
$sxLog->recordsqllogger ($e);
return false;
}
}
/**
* Binding Parameter procedure
*
* @param string $sql SQL statement
* @param int $location question mark position
* @param mixed $var replacement variables
* @param type of string $type substitution
*/
Public Function Bindparam (& $sql, $location, $var, $type = ' STRING ') {
Switch ($type) {
String
Default: //Use String type
Case ' STRING ':
$var = Addslashes ($var); Escape
$var = "'". $var. "'"; Plus single quotation marks. string insertion in SQL statements must be enclosed in single quotation marks
Break
Case ' INTEGER ':
Case ' INT ':
$var = (int) $var; Cast to int
You can also add more types:
}
Find the location of the question mark
For ($i =1, $pos = 0; $i <= $location; $i + +) {
$pos = Strpos ($sql, '? ', $location + 1);
//}
Replace question mark
$sql = substr ($sql, 0, $pos). $var. substr ($sql, $pos + 1);
return $sql;
}
Splicing SQL statement parameter bindings