php 使用php简单搜索MySQL数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23024114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:26:16  来源:igfitidea点击:

Simple search MySQL database using php

php

提问by TimelordViktorious

I currently have a small php script that searches a database based on user input. There is an html file that has one field that is used for entry of search strings into the database. Essentially, you can search for employees.

我目前有一个小的 php 脚本,可以根据用户输入搜索数据库。有一个 html 文件,其中有一个字段用于将搜索字符串输入到数据库中。基本上,您可以搜索员工。

It is supposed to retrieve the employee result, if found, and a 'no employee found' message, if not.

如果找到,它应该检索员工结果,如果没有,则应该检索“未找到员工”消息。

For some reason, however, no matter the search, the query returns every employee in the database.

但是,出于某种原因,无论进行何种搜索,查询都会返回数据库中的每个员工。

I've been working on this for over an hour, and I am honestly stumped. It may be a simple error but I could do with some help.

我已经为此工作了一个多小时,老实说我很难过。这可能是一个简单的错误,但我可以得到一些帮助。

<?php
    $con= new mysqli("localhost","root","","Employee");
    $name = $_post['search'];
    //$query = "SELECT * FROM employees
   // WHERE first_name LIKE '%{$name}%' OR last_name LIKE '%{$name}%'";

    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

$result = mysqli_query($con, "SELECT * FROM employees
    WHERE first_name LIKE '%{$name}%' OR last_name LIKE '%{$name}%'");

while ($row = mysqli_fetch_array($result))
{
        echo $row['first_name'] . " " . $row['last_name'];
        echo "<br>";
}
    mysqli_close($con);
    ?>

Any help appreciated.

任何帮助表示赞赏。

Thanks.

谢谢。

回答by Jacob

You need to use $_POSTnot $_post.

您需要使用$_POSTnot $_post

回答by Tarik Billa

First add HTML code:

首先添加HTML代码:

<form action="" method="post">
<input type="text" name="search">
<input type="submit" name="submit" value="Search">
</form>

Now added PHP code:

现在添加了 PHP 代码:

<?php
$search_value=$_POST["search"];
$con=new mysqli($servername,$username,$password,$dbname);
if($con->connect_error){
    echo 'Connection Faild: '.$con->connect_error;
    }else{
        $sql="select * from information where First_Name like '%$search_value%'";

        $res=$con->query($sql);

        while($row=$res->fetch_assoc()){
            echo 'First_name:  '.$row["First_Name"];


            }       

        }
?>

回答by CarbonCoding

`

`

require_once('functions.php');

$errors = FALSE;
$errorMessage = "";

if(mysqli_connect_error()){
  $errors = TRUE;
  $errorMessage .= "There was a connection error <br/>";
  errorDisplay($errorMessage);
  die($errors);
} else if($errors != "TRUE"){
  $errors .= FALSE;
}

if(isset(mysqli_real_escape_string($_POST['search']))){
  $search = mysqli_real_escape_string($_POST['search']);
  search(search);
}



?>
<?php
//This is functions.php
function search($searchQuery){
  echo "<div class="col-md-10 col-md-offset-1">";
  $searchTerm
  $query = query("SELECT * FROM `index` WHERE `keywords` LIKE '".$searchTerm."' ");
  while($row = mysqli_fetch_array($query)){
    $results = <<< DELIMITER
      <div class="result col-md-12">
        <a href="index.php?search={$row['id']}"> {$row['Title']} </a>
        <p class="searchDesc">{$row['description']}</p>
      </div>
DELIMITER;
    echo $results;
  }
  echo "</div>";
}

function errorDisplay($msg){
  if(!isset($_SESSION['errors'])){
    $_SESSION['errors'] = $msg;
    showError($msg);
  } else if() {
    $_SESSION['errors'] .= $msg . "<br>";
    showError($msg);
  }
}
function showError($msg) {
  return $msg;
  unset($_SESSION['errors']);
}


?>`
Perhaps That Helps?

回答by user11504985

If you do mysqli_fetch_array(), you must put integer in $row index ex.($row[3]).If you read $row['id']or $row['example'], you must use mysqli_fetch_assoc.

如果你这样做mysqli_fetch_array(),你必须把整数放入$row index ex.($row[3])。如果你读$row['id']$row['example'],你必须使用mysqli_fetch_assoc

回答by Ssekiziyivu Godfrey

Just with the above answer I hope it was the problem.

只是有了上面的答案,我希望这是问题所在。

$_POST['search']instead of $_post['search']

$_POST['search']代替 $_post['search']

And again use LIKE '%$name%'instead of LIKE '%{$name}%'

并再次使用LIKE '%$name%'代替LIKE '%{$name}%'

回答by Ssekiziyivu Godfrey

This is a better code that will help you through.
With your database, but rather, I have used mysql not mysqli
Enjoy it.

这是一个更好的代码,可以帮助您完成。
用你的数据库,倒是我用的是mysql,不是mysqli
好好享受吧。

<body>

<form action="" method="post">

  <input name="search" type="search" autofocus><input type="submit" name="button">

</form>

<table>
  <tr><td><b>First Name</td><td></td><td><b>Last Name</td></tr>

<?php

$con=mysql_connect('localhost', 'root', '');
$db=mysql_select_db('employee');


if(isset($_POST['button'])){    //trigger button click

  $search=$_POST['search'];

  $query=mysql_query("select * from employees where first_name like '%{$search}%' || last_name like '%{$search}%' ");

if (mysql_num_rows($query) > 0) {
  while ($row = mysql_fetch_array($query)) {
    echo "<tr><td>".$row['first_name']."</td><td></td><td>".$row['last_name']."</td></tr>";
  }
}else{
    echo "No employee Found<br><br>";
  }

}else{                          //while not in use of search  returns all the values
  $query=mysql_query("select * from employees");

  while ($row = mysql_fetch_array($query)) {
    echo "<tr><td>".$row['first_name']."</td><td></td><td>".$row['last_name']."</td></tr>";
  }
}

mysql_close();
?>