只准备一次语句的动态准备语句现成函数
Dynamic Prepared Statement Readymade Function that Prepare Statement ONLY ONCE
I want a function that prepare statement only once. ( Not every time whenever function called for the same Query ).
Because One of Advantage of Prepared Statement is: preparation on the query is done only once (although the statement is executed multiple times)
因此,每当我调用此函数时,它每次都会准备语句,这是不合适的,因为我们会错过利用准备语句的最大优势之一。因此,我将条件 if( !empty($stmtName) and !isset($GLOBALS[$stmtName]) )
设置为 Check Statement is Already or not。 (如果没有,那么只有 Statement 才会准备)使用这个我们将覆盖那个优势。 但是,它生成绑定参数失败错误。
我的函数在这里...
function qryInsert( $stmtName, $table, $field, $params, $formats )
{
$query = " INSERT INTO ".$table
." ".( (isset($field) and !empty($field)) ? " ( ".(implode(", ",$field))." ) " : " " ). " "
." VALUES( ". implode(", ", array_map(function($val) { return "?"; }, $field))." ) ";
/*if(!isset($con) or empty($con))
{
$con = $this->connection();
}*/
$a_params = array();
$a_params = array();
$param_type = '';
$n = count($formats);
for($i = 0; $i < $n; $i++)
{
$param_type .= $formats[$i];
}
$a_params[] = & $param_type;
for($i = 0; $i < $n; $i++)
{
$a_params[] = & $params[$i];
}
if( !empty($stmtName) and !isset($GLOBALS[$stmtName]) )
{
$GLOBALS[$stmtName] = $GLOBALS['con']->prepare($query);
// $stmt = $con->prepare($query);
}
if(!($GLOBALS[$stmtName]))
{
echo " Prepare failed: (" . $con->errno . ") " . $con->error; // . " <br> Query : <span style='color:tomato;'> ".$query." </span>"
}
else
{
if(!(call_user_func_array(array($GLOBALS[$stmtName], 'bind_param'), $a_params)))
{
echo "Binding parameters failed: (" . $GLOBALS[$stmtName]->errno . ") " . $GLOBALS[$stmtName]->error;
}
else
{
if(!($GLOBALS[$stmtName]->execute()))
{
echo "Execute failed: (" . $GLOBALS[$stmtName]->errno . ") " . $GLOBALS[$stmtName]->error;
}
else
{
if($meta = $GLOBALS[$stmtName]->result_metadata())
{
while ($field = $meta->fetch_field())
{
$columns[] = &$row[$field->name];
}
if(call_user_func_array(array($GLOBALS[$stmtName], 'bind_result'), $columns))
{
while ($GLOBALS[$stmtName]->fetch())
{
foreach($row as $key => $val)
{
$x[$key] = $val;
}
$results[] = $x;
}
}
else
{
echo " Error occur while Bindig Result...";
}
}
}
}
}
$GLOBALS[$stmtName]->close();
return $results;
}
输入:
qryInsert("insStud", "student_master", array("roll_no","name"), array(21,"Mahetab"), array("i","s"));
qryInsert("insStud", "student_master", array("roll_no","name"), array(8,"Sahil"), array("i","s"));
qryInsert("insStud", "student_master", array("roll_no","name"), array(10,"Mahammad"), array("i","s"));
输出:
First time Record will Insert...
After that raised Binding Parameter failed error...
没有这个条件 if( !empty($stmtName) and !isset($GLOBALS[$stmtName]) )
我的代码工作正常...没有任何问题...因为它每次都会准备语句
I used $GLOBALS variable so that whenever function called it uses same GLOBALS variable Otherwise function Perform operation with their private variable which doesn't work properly
问题已解决...
只想删除 $GLOBALS[$stmtName]->close();
语句...
因为每次调用函数时它都会关闭语句...
因此,绑定参数失败...
I want a function that prepare statement only once. ( Not every time whenever function called for the same Query ).
Because One of Advantage of Prepared Statement is: preparation on the query is done only once (although the statement is executed multiple times)
因此,每当我调用此函数时,它每次都会准备语句,这是不合适的,因为我们会错过利用准备语句的最大优势之一。因此,我将条件 if( !empty($stmtName) and !isset($GLOBALS[$stmtName]) )
设置为 Check Statement is Already or not。 (如果没有,那么只有 Statement 才会准备)使用这个我们将覆盖那个优势。 但是,它生成绑定参数失败错误。
我的函数在这里...
function qryInsert( $stmtName, $table, $field, $params, $formats )
{
$query = " INSERT INTO ".$table
." ".( (isset($field) and !empty($field)) ? " ( ".(implode(", ",$field))." ) " : " " ). " "
." VALUES( ". implode(", ", array_map(function($val) { return "?"; }, $field))." ) ";
/*if(!isset($con) or empty($con))
{
$con = $this->connection();
}*/
$a_params = array();
$a_params = array();
$param_type = '';
$n = count($formats);
for($i = 0; $i < $n; $i++)
{
$param_type .= $formats[$i];
}
$a_params[] = & $param_type;
for($i = 0; $i < $n; $i++)
{
$a_params[] = & $params[$i];
}
if( !empty($stmtName) and !isset($GLOBALS[$stmtName]) )
{
$GLOBALS[$stmtName] = $GLOBALS['con']->prepare($query);
// $stmt = $con->prepare($query);
}
if(!($GLOBALS[$stmtName]))
{
echo " Prepare failed: (" . $con->errno . ") " . $con->error; // . " <br> Query : <span style='color:tomato;'> ".$query." </span>"
}
else
{
if(!(call_user_func_array(array($GLOBALS[$stmtName], 'bind_param'), $a_params)))
{
echo "Binding parameters failed: (" . $GLOBALS[$stmtName]->errno . ") " . $GLOBALS[$stmtName]->error;
}
else
{
if(!($GLOBALS[$stmtName]->execute()))
{
echo "Execute failed: (" . $GLOBALS[$stmtName]->errno . ") " . $GLOBALS[$stmtName]->error;
}
else
{
if($meta = $GLOBALS[$stmtName]->result_metadata())
{
while ($field = $meta->fetch_field())
{
$columns[] = &$row[$field->name];
}
if(call_user_func_array(array($GLOBALS[$stmtName], 'bind_result'), $columns))
{
while ($GLOBALS[$stmtName]->fetch())
{
foreach($row as $key => $val)
{
$x[$key] = $val;
}
$results[] = $x;
}
}
else
{
echo " Error occur while Bindig Result...";
}
}
}
}
}
$GLOBALS[$stmtName]->close();
return $results;
}
输入:
qryInsert("insStud", "student_master", array("roll_no","name"), array(21,"Mahetab"), array("i","s"));
qryInsert("insStud", "student_master", array("roll_no","name"), array(8,"Sahil"), array("i","s"));
qryInsert("insStud", "student_master", array("roll_no","name"), array(10,"Mahammad"), array("i","s"));
输出:
First time Record will Insert...
After that raised Binding Parameter failed error...
没有这个条件 if( !empty($stmtName) and !isset($GLOBALS[$stmtName]) )
我的代码工作正常...没有任何问题...因为它每次都会准备语句
I used $GLOBALS variable so that whenever function called it uses same GLOBALS variable Otherwise function Perform operation with their private variable which doesn't work properly
问题已解决...
只想删除 $GLOBALS[$stmtName]->close();
语句...
因为每次调用函数时它都会关闭语句...
因此,绑定参数失败...