PHP Binding a Wildcard

后端 未结 4 1620
甜味超标
甜味超标 2020-12-10 09:35

Hope Somebody will help me about how I bind a parameter in mysqli when a multiple character wildcard needs to be next to the variable value. I found that it worked for me wh

相关标签:
4条回答
  • 2020-12-10 10:01
       $sql="SELECT item_title FROM item WHERE item_title LIKE concat ('%',?,'%') ";
    
    0 讨论(0)
  • 2020-12-10 10:02

    have one question mark per bind variable. For example prepare(SELECT item_title FROM item WHERE item_title LIKE ? and name2 like ? and ...) The nice thing about prepared statements is that you don't need to worry about quoting the variables.

    Then bind all parameters like bind_param("ss...", $param1, $param2, ....);

    0 讨论(0)
  • 2020-12-10 10:16

    You can do like this way:

    $sql = "SELECT item_title FROM item WHERE item_title LIKE ? ";
    

    and then

    $title_new =  '%'.$title.'%';
    mysqli_stmt_bind_param($stmt, 's', $title_new);    
    

    Updated based on user's comment

    To implement below SQL

     s2.subject_name LIKE '%$keyword%' OR c.city_name LIKE '%$keyword%' 
    

    Use below MySqli statement s2.subject_name LIKE ? OR c.city_name LIKE ?

     $keyword = '%'.$keyword.'%';
     mysqli_stmt_bind_param($stmt, 'ss', $keyword, $keyword);
    
    0 讨论(0)
  • 2020-12-10 10:20

    You can bind only data literals not arbitrary query parts.
    So, prepare your literal first

    $var = "%$var%";
    $sql = "SELECT item_title FROM item WHERE item_title LIKE ?";
    
    0 讨论(0)
提交回复
热议问题