Created
January 1, 2011 05:41
-
-
Save shameerc/761581 to your computer and use it in GitHub Desktop.
This is a part of mysqli class for prepared statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param mixed $query Query to be prepared for executing | |
* multiple times | |
* Usage "SELECT * FROM table_name WHERE column = ? " | |
* "INSERT INTO table_name(field1,field2) VALUES(?,?) "; | |
* returns true if the query is a valid mysql statement else throws an | |
* exception | |
* | |
*/ | |
function prepare($query) | |
{ | |
if(!($this->stmt = $this->isCached($query))) { | |
$this->stmt = $this->mysqli->prepare($query); | |
$this->cacheQuery($query, $this->stmt); | |
} | |
if(!($this->stmt instanceof mysqli_stmt)) | |
throw new Exception ('Invalide query : '.$this->mysqli->error); | |
else { | |
return true; | |
} | |
} | |
/** | |
* | |
* @param mixed $values Array or single value that need to be processed | |
* using already created prepared statment. | |
* @param boolean $isManip Whether the query is a data manipulation | |
* query or not. | |
* @return mixed $output If @see $isManip is true return value will be | |
* boolean else it will be an array | |
* Usage execute(value); or execute(array(value1,value2), true); | |
*/ | |
function execute($values, $isManip = false, &$stmt = '' ) | |
{ | |
$stmt = ($stmt == '') ? $this->stmt : $stmt; | |
//check whether the statment is a valid prepared statment. | |
if(!($stmt instanceof mysqli_stmt)) | |
throw new Exception ('Invalide query : '.$this->mysqli->error ); | |
$ptype =''; | |
//Check if $values is an array or not | |
if(is_array($values)) { | |
// Check the datatype of each value in the array and generate | |
// corresponding typestring $ptype | |
foreach($values as $value) { | |
if(is_int($value)) | |
$ptype .= 'i'; | |
elseif(is_float($value)) | |
$ptype .='d'; | |
else | |
$ptype .= 's'; | |
$params[] = $value; | |
} | |
} | |
else { | |
if(is_int($values)) | |
$ptype .= 'i'; | |
elseif(is_float($values)) | |
$ptype .='d'; | |
else | |
$ptype .= 's'; | |
$params[] = $values; | |
} | |
//Combine the $params array, $this->stmt and $ptype to be passed as | |
//an argument for call_user_func_array() | |
array_unshift($params,$stmt ,$ptype); | |
call_user_func_array('mysqli_stmt_bind_param', $this->refValues($params)); | |
//Execute the prepared statment | |
$output = $stmt->execute(); | |
// Build the output array if its not a data manipulation query | |
if($isManip === false) { | |
$data = $stmt->result_metadata(); | |
$fields = array(); | |
$out = array(); | |
$fields[0] = $stmt; | |
$count = 1; | |
while($field = mysqli_fetch_field($data)) { | |
$fields[$count] = &$out[$field->name]; | |
$count++; | |
} | |
call_user_func_array('mysqli_stmt_bind_result', $fields); | |
$i=0; | |
while($stmt->fetch()) { | |
$result[] = $out; | |
} | |
if(count($result) == 1 ) | |
$output = array_pop($result); | |
else | |
$output = $result; | |
} | |
if(isset($output)) | |
return $output; | |
else | |
return false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment