douhuan7862 2017-06-26 17:16
浏览 54
已采纳

无法将LIKE添加到我的SQL查询中

include("config.php");
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

if(!is_numeric($page_number)){
  header('HTTP/1.1 500 Invalid page number!');
  exit();
}
session_start();
$position = (($page_number-1) * $item_per_page);

if(!empty($_SESSION['type'])){
  $typesql = $_SESSION['type'];
  $results = $mysqli->prepare("SELECT name, location, score, img, id, type FROM artists WHERE type = ? ORDER BY score DESC LIMIT ?, ?");
  $results->bind_param("sii", $typesql, $position, $item_per_page);
  $results->execute();
  $results->bind_result($name, $location, $score, $img, $id, $type);
} else {
  $results = $mysqli->prepare("SELECT name, location, score, img, id, type FROM artists ORDER BY score DESC LIMIT ?, ?");
  $results->bind_param("ii", $position, $item_per_page);
  $results->execute();
  $results->bind_result($name, $location, $score, $img, $id, $type);
}

// Le fetch
  while($results->fetch()){
    //my cards here
  }
  ?>

I'm looking for ultimately hooking my search box to this query which is not working, I've tried to add alter the query to the below for testing purpose:

SELECT name, location, score, img, id, type FROM artists WHERE name LIKE '%etc%' ORDER BY score DESC LIMIT ?, ?

and I do have a name that has "etc" in it but the result I get is:

Call to a member function bind_param() on boolean in

How do I change this query to bind the $_GET result from the searchbox to it, the website is Setch.me

  • 写回答

1条回答 默认 最新

  • donglinyi4313 2017-06-26 17:23
    关注

    As in this question you can use LIKE with a bound parameter in mysqli:

    $param = "%{$_POST['searchphrase']}%";
    $stmt = $mysqli->prepare("SELECT name, location, score, img, id, type FROM artists WHERE name LIKE ?");
    $stmt->bind_param("s", $param);
    $stmt->execute();
    $stmt->bind_result($name, $location, $score, $img, $id, $type);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示