MYSQL 搜索结果 [英] MYSQL search result

查看:32
本文介绍了MYSQL 搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,其中有用户会从 MySQL 中搜索名称.

首先 mysql 应该在 first_name 中搜索,然后去 last_name 寻找相同的搜索选项,然后显示结果.(来自名字和姓氏)

我试过了,但它只显示了名字的结果

请帮帮我.

代码如下:-

尝试{$keyword = trim($_GET["keyword"]);如果 ($keyword <> "" ) {$sql = "SELECT * FROM tbl_contacts WHERE 1 AND "." (first_name LIKE :keyword) ORDER BY first_name ";$stmt = $DB->prepare($sql);$stmt->bindValue(":keyword", $keyword."%");} elseif ($keyword <> "" ) {$sql = "SELECT * FROM tbl_contacts WHERE 1 AND "." (last_name LIKE :keyword) ORDER BY first_name ";$stmt = $DB->prepare($sql);$stmt->bindValue(":keyword", $keyword."%");}别的 {$sql = "SELECT * FROM tbl_contacts WHERE 1 ORDER BY first_name ";$stmt = $DB->prepare($sql);}$stmt->execute();$total_count = count($stmt->fetchAll());

解决方案

尽量避免以其他方式发布相同问题,编辑相同问题.

您在 MYSQL OR 不工作

中问了同样的问题

希望这真的能帮到你:-

尝试{$keyword = trim($_GET["keyword"]);如果 ($keyword <> "" ) {$sql = "SELECT * FROM tbl_contacts WHERE 1 AND "." (first_name LIKE :keyword OR last_name LIKE :keyword) ORDER BY first_name ";$stmt = $DB->prepare($sql);$stmt->bindValue(":keyword", $keyword."%");}别的 {$sql = "SELECT * FROM tbl_contacts WHERE 1 ORDER BY first_name ";$stmt = $DB->prepare($sql);}$stmt->execute();$total_count = count($stmt->fetchAll());

我也回答了你的新问题https://stackoverflow.com/a/44859408/7678788>

I have a code, in which there are users will search for the name from MySQL.

First the mysql should search in first_name, then go to last_name for the same search option and then display results. (From both First_name and Last_name)

I tried but it showed me only the results from first name

Please help me.

Here is the code:-

try {
  $keyword = trim($_GET["keyword"]);
  if ($keyword <> "" ) {
    $sql = "SELECT * FROM tbl_contacts WHERE 1 AND "
            . " (first_name LIKE :keyword) ORDER BY first_name ";
    $stmt = $DB->prepare($sql);

    $stmt->bindValue(":keyword", $keyword."%");

  } elseif ($keyword <> "" ) {
    $sql = "SELECT * FROM tbl_contacts WHERE 1 AND "
            . " (last_name LIKE :keyword) ORDER BY first_name ";
    $stmt = $DB->prepare($sql);

    $stmt->bindValue(":keyword", $keyword."%");

  }else {
    $sql = "SELECT * FROM tbl_contacts WHERE 1 ORDER BY first_name ";
    $stmt = $DB->prepare($sql);
  }

  $stmt->execute();
  $total_count = count($stmt->fetchAll());

解决方案

Try to avoid posting same question in other ways, edit the same question.

You asked the same question in MYSQL OR not working

Hope this will really help you:-

try {
  $keyword = trim($_GET["keyword"]);
  if ($keyword <> "" ) {
    $sql = "SELECT * FROM tbl_contacts WHERE 1 AND "
            . " (first_name LIKE :keyword OR last_name LIKE :keyword) ORDER BY first_name ";
    $stmt = $DB->prepare($sql);

    $stmt->bindValue(":keyword", $keyword."%");
  }else {
    $sql = "SELECT * FROM tbl_contacts WHERE 1 ORDER BY first_name ";
    $stmt = $DB->prepare($sql);
  }

  $stmt->execute();
  $total_count = count($stmt->fetchAll());

I have also answered your new repeated question https://stackoverflow.com/a/44859408/7678788

这篇关于MYSQL 搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆