【问题标题】:composed mysql query working but if parametrized in mysqli don't working组成的 mysql 查询工作,但如果在 mysqli 中参数化不工作
【发布时间】:2013-02-23 12:32:52
【问题描述】:

我有一个包含 3 个字词(搜索参数、邮政编码和活动类型)的搜索页面

我已经做了一个函数来编写sql:(这不是真正的函数,只是一个简化的函数)。您可以将它与要过滤的参数一起使用,也可以不使用参数来获取所有参数。

function get_items($search="",$postal_code="",$activity=""){
global $db; //this is the $db=new mysqli(...) in other include file
$where="";
if ($s!=""){
    $s="%".$search."%";
    $where=" AND ((item.name like '".$s."') OR (item.description like '".$s."'))";
}
if($postal_code!=""){
    if (strlen($postal_code)==5){
         $where=" AND (item.postal_code like '".$postal_code."')";
    }
}

if($activity!=""){
    if (m_is_integer($postal_code)){ //m_is_integer returns true if is an integer
         $where=" AND (item.activity =".$activity.")";
    }
}
$sql="select ....... from -..... where .....".$where." order by ......"
//yes, I know I don't need to prepare the query 
$stmt=$db->prepare($sql); 
$result=$stmt->execute();
$stmt->store_result();
$item_array=Array();
if (($result!=false) && ($stmt->num_rows>0)){
     //do things and populate the array $item_array
}
$stmt->close();
return $item_array;
}

这个函数有效,sql 是正确组合的,你输入任何参数或不输入,并返回一个项目数组。

我想进行参数化查询,这是我的方法:

function get_items_parametrized($search="",$postal_code="",$activity=""){
global $db; //this is the $db=new mysqli(...) in other include file
$where="";
$bind_array=Array();
if ($s!=""){
    $s="%".$search."%";
    $where=" AND ((item.name like ?) OR (item.description like ?))";
    $bii=Array("s",$s);
    $bind_array[]=$bii;
    $bii=Array("s",$s);
    $bind_array[]=$bii;
}
if($postal_code!=""){
    if (strlen($postal_code)==5){
         $where=" AND (item.postal_code like ?)";
         $bii=Array("s",$postal_code); //yes, is a string in the database
         $bind_array[]=$bii;
    }
}

if($activity!=""){
    if (m_is_integer($postal_code)){ //m_is_integer returns true if is an integer
         $where=" AND (item.activity = ?)";
         $bii=Array("i",$activity);
         $bind_array[]=$bii;
    }
}
$sql="select ....... from -..... where .....".$where." order by ......"
$stmt=$db->prepare($sql);
//go to bind data to search
$bind_type="";
$bind_params=Array();

foreach($bind_array as $b){
    $bind_type.=$b[0];
    $bind_params[]=$b[1];
    /* Approach 1: */
    $stmt->bind_param($b[0],$b[1]); 
}
/* Approach 2: */
$stmt->bind_param($bind_type,$bind_params); 
$result=$stmt->execute();
$stmt->store_result();
$item_array=Array();
if (($result!=false) && ($stmt->num_rows>0)){
     //do things and populate the array $item_array
}
$stmt->close();
return $item_array;
}

这个函数总是返回一个空的 $item_array Array () 而不是一个 Array(Array(),Array()) 如果我不绑定结果是可能的,执行不会返回任何结果。

我也尝试过:

/* attempt 3 */
$data=Array();
$data[0]="";
foreach($bind_array as $b){
    $data[]=$b1;
    $bind_type.=$b[0];
}
$data[0]=$bind_type;

组成一个类似 ('ssi',$s,$postal_code,$activity) 的数组来调用 call_user_func_array():

call_user_func_array(array(&$stmt, 'bind_param'), $data);

我也试试:

call_user_func_array(array($stmt, 'bind_param'), $data);

而且这种方法仍然没有返回任何数据。

我现在可以尝试什么使其适用于参数化查询?

欢迎任何帮助:D

【问题讨论】:

  • bind_param 要求参数为引用,call_user_func_array 不再传递引用。

标签: php mysqli sql-parametrized-query


【解决方案1】:

答案很简单:不要在准备好的语句中使用 mysqli
它不适用于准备好的语句。
请改用 PDO
答案。 Mysqli 是你的问题,你必须解决它。

使用 PDO,您的代码将缩短 3 倍并且可以正常工作。

如果您想坚持使用 mysqli,另一种方法是摆脱准备好的语句并实现自己的占位符,但需要一些知识。但是,它会让您更轻松地构建条件查询:

$w = array();
$where = '';
if ($one) $w[] = $db->parse("one = ?s",$one); 
if ($two) $w[] = $db->parse("two IN (?a)",$two);
if ($tre) $w[] = $db->parse("tre <= ?i",$tre);
if (count($w)) $where = "WHERE ".implode(' AND ',$w);
$data = $db->getArr("SELECT * FROM table ?p LIMIT ?i,?i",$where, $start,$per_page);

【讨论】:

  • 对于“我有时间”的解决方案,我会尝试,但现在无法使用 pdo,它在服务器中被禁用,并且所有应用程序都使用 mysqli(我不打算制作一个)仅用于一个功能的连接字符串)。谢谢
  • 你可以使用safemysql。它可以让你构建条件查询,它使用的是 mysqli。我将举个例子来回答。
  • 我会尝试,但我不明白为什么我的参数化查询不工作。
猜你喜欢
  • 2023-04-05
  • 2013-03-14
  • 2018-07-19
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 2014-09-07
  • 2016-03-14
相关资源
最近更新 更多