Php 使用bind_result&;fetch()或store_result()而不是get_result
如何将此函数更改为另一个函数。我不想使用get\u结果 我在网上搜索了一下,但找不到对我有帮助的答案Php 使用bind_result&;fetch()或store_result()而不是get_result,php,mysqli,prepared-statement,Php,Mysqli,Prepared Statement,如何将此函数更改为另一个函数。我不想使用get\u结果 我在网上搜索了一下,但找不到对我有帮助的答案 public function Select($Table_Name, $Conditions='' ,$Array_Conditions_Limit=NULL , $OrderBy='', $Limit='', $Selected_Fields='*') { $Query = "SELECT ".$Selected_Fields." FROM ".$Table_Name; if
public function Select($Table_Name, $Conditions='' ,$Array_Conditions_Limit=NULL , $OrderBy='', $Limit='', $Selected_Fields='*')
{
$Query = "SELECT ".$Selected_Fields." FROM ".$Table_Name;
if(!empty($Conditions))
$Query .= " WHERE ".$Conditions;
if(!empty($OrderBy))
$Query .= " ORDER BY ".$OrderBy;
if(!empty($Limit))
$Query .= " LIMIT ".$Limit;
$Statment = $this->ConnectionResult->prepare($Query);
if(isset($Array_Conditions_Limit) )
{
$Statment = $this->DynamicBindVariables($Statment, $Array_Conditions_Limit);
$Statment->execute();
return $Statment->get_result();
}
else
$Statment->execute();
return $Statment->get_result();
}
这也可以用于动态绑定变量
private function DynamicBindVariables($Statment, $Params)
{
if (is_array($Params) && $Params != NULL)
{
// Generate the Type String (eg: 'issisd')
$Types = '';
foreach($Params as $Param)
{
$Types .= $this->GetType($Param);
}
// Add the Type String as the first Parameter
$Bind_names[] = $Types;
// Loop thru the given Parameters
for ($i=0; $i<count($Params);$i++)
{
$Bind_name = 'bind' . $i;
// Add the Parameter to the variable
$$Bind_name = $Params[$i];
// Associate the Variable as an Element in the Array
$Bind_names[] = &$$Bind_name;
}
// Call the Function bind_param with dynamic Parameters
call_user_func_array(array($Statment,'bind_param'), $Bind_names);
}
elseif(isset($Params) && !empty($Params))
{
$Types = '';
$Types .= $this->GetType($Params);
$Statment->bind_param($Types ,$Params);
}
return $Statment;
}
首先,我发现这种方法毫无用处。实际上,您正在将一个好的SQL语句分解成一些匿名部分
"SELECT * FROM post WHERE post_category=?"
看起来比你的匿名参数好多了,没人知道
'post','post_category=?'
人们一眼就能看出第一句话要做什么。第二天我也不知道。更不用说它是极端的:
'post','post_category=?',NULL, NULL, 'username, password'
因此,我宁愿推荐一个只接受两个参数的函数,而不是这个幼稚园查询生成器——查询本身和绑定数据的数组:
$myresult = Select("SELECT * FROM post WHERE post_category=?", [2]);
为了使它更有用,我将使用单独的函数来获得不同的结果类型,使第二行的fetch\u object()
过时(但是,说到对象,它们对于表示表行是完全无用的)。例如:
$row = $db->selectRow("SELECT * FROM post WHERE post_category=?", [2]);
看:它简明易懂
作为下一步,您可能希望实现更多占位符类型,以允许ORDER BY子句的字段也参数化:
$data = $db->getAll('id','SELECT * FROM t WHERE id IN (?a) ORDER BY ?n', [1,2],'f');
您可以在我的中看到它的工作原理,以及其他函数和用例,我会回答,但这个无用的
select()
函数让我反感。我不知道为什么这种[不方便且完全不可用的]方法对PHP用户如此有吸引力。你能告诉我更多关于它的信息吗?为什么它毫无用处?因为在读起来几乎像自然英语的出色SQL语言中,你制造了一个丑陋的弗兰肯斯坦,没有人能读(读起来是颠倒的),它让你只运行愚蠢的查询,让任何复杂的事情都不可能发生。所有这些都是为了节省键入三个单词的时间。您有什么改进的建议吗?我的建议是保留SQL,而不是分解它。感谢我学到的有用的东西。您能给我一个例子来解决这个问题吗?替换get_result在这里您会发现一些
$data = $db->getAll('id','SELECT * FROM t WHERE id IN (?a) ORDER BY ?n', [1,2],'f');