यह है कि मैं क्या mysqli पैरामीटर के एक चर राशि के साथ विवरण तैयार करने के लिए इस्तेमाल करते हैं। यह मैंने लिखा एक वर्ग का हिस्सा है। यह आपके लिए आवश्यकतानुसार अधिक है, लेकिन यह आपको सही दिशा दिखाना चाहिए।
public function __construct($con, $query){
$this->con = $con;
$this->query = $query;
parent::__construct($con, $query);
//We check for errors:
if($this->con->error) throw new Exception($this->con->error);
}
protected static $allowed = array('d', 'i', 's', 'b'); //allowed types
protected static function mysqliContentType($value) {
if(is_string($value)) $type = 's';
elseif(is_float($value)) $type = 'd';
elseif(is_int($value)) $type = 'i';
else throw new Exception("type of '$value' is not string, int or float");
return $type;
}
//This function checks if a given string is an allowed mysqli content type for prepared statement (s, d, b, or i)
protected static function mysqliAllowedContentType($s){
return in_array($s, self::$allowed);
}
public function feed($params){
//These should all be empty in case this gets used multiple times
$this->paramArgs = array();
$this->typestring = '';
$this->params = $params;
$this->paramArgs[0] = '';
$i = 0;
foreach($this->params as $value){
//We check the type:
if(is_array($value)){
$temp = array_keys($value);
$type = $temp[0];
$this->params[$i] = $value[$type];
if(!self::mysqliAllowedContentType($type)){
$type = self::mysqliContentType($value[$type]);
}
}
else{
$type = self::mysqliContentType($value);
}
$this->typestring .= $type;
//We build the array of values we pass to the bind_params function
//We add a refrence to the value of the array to the array we will pass to the call_user_func_array function. Thus say we have the following
//$this->params array:
//$this->params[0] = 'foo';
//$this->params[1] = 4;
//$this->paramArgs will become:
//$this->paramArgs[0] = 'si'; //Typestring
//$this->paramArgs[1] = &$this->params[0];
//$this->paramArgs[2] = &$this->params[1].
//Thus using call_user_func_array will call $this->bind_param() (which is inherented from the mysqli_stmt class) like this:
//$this->bind_param('si', &$this->params[0], &$this->params[1]);
$this->paramArgs[] = &$this->params[$i];
$i++;
}
unset($i);
$this->paramArgs[0] = $this->typestring;
return call_user_func_array(array(&$this, 'bind_param'), $this->paramArgs);
}
आप इस तरह इसका इस्तेमाल:
$prep = new theClassAboveHere($mysqli, $query);
$prep->feed(array('string', 1, array('b', 'BLOB DATA'));
वर्ग mysqli_stmt वर्ग का विस्तार करना चाहिए।
मुझे आशा है कि यह आपको सही दिशा में मदद करेगा।
यदि आप नहीं चाहते हैं कि मैं पूरी कक्षा भी पोस्ट कर सकता हूं, इसमें परिवर्तनीय परिणाम बाध्यकारी शामिल हैं।
स्रोत
2009-04-27 13:38:12
@altCongnito 'array_insert() वास्तव में मौजूद है? –
आपने सरणी – pmrotule