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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改