Splicing SQL statement parameter bindings

Source: Internet
Author: User

/**
* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.