如何在数据库中通过mysqli从输入中搜索值

时间:2017-03-06 14:18:57

标签: php mysqli

我想在使用值的第一个字符时显示我的数据库值。 让我用我的网站来描述它 我有一个主页输入的网站,其中用户输入为火车号码。我需要那个用户类型的火车没有。他从我存储的数据库中获得了火车名称。

3 个答案:

答案 0 :(得分:0)

我没有真正得到你的问题,但从我得到的,你可以使用这个查询:

SELECT * FROM table_name WHERE column LIKE '%value_here%'

答案 1 :(得分:0)

试试这个

if (isset($_POST['searchVal'])) {
$searchquery = $_POST['searchVal']; //input for search

//count table to see if any row/column has the train number

$searcher = mysqli_query("SELECT COUNT(*) FROM tablename WHERE trainno LIKE '%$searchquery%'") or die("could not search");


$count = mysqli_num_rows($searcher);

if ($count == 0) {
    echo "No results found";
} else {

    //while loop to select row with train name
    while($row = msqli_fetch_array($query)) {

        $trainnname = $row['trainname'];//name of row containing train name

        echo $trainname;
    }
  } 
}

希望你了解这是如何运作的

答案 2 :(得分:0)

检查此代码。我认为它会对您有所帮助

<html>
 <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP, jQuery search demo</title>
<link rel="stylesheet" type="text/css" href="my.css">

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("input").keyup(function () {
            $('#results').html('');
            var searchString = $("#search_box").val();
            var data = 'search_text=' + searchString;
            if (searchString) {
                $.ajax({
                    type: "POST",
                    url: 'search.php',
                    data: data,
                    dataType: 'text',
                    async: false,
                    cache: false,
                    success: function (result) {
                        $('#results').html(result);
                        //window.location.reload();

                    }
                });
            }
        });
    });
  </script>

 </head>
  <body>
 <div id="container">
 <div style="margin:20px auto; text-align: center;">
    <form method="post" action="do_search.php">
        <input type="text" name="search" id="search_box" class='search_box'/>
        <input type="submit" value="Search" class="search_button"/><br/>
    </form>
</div>
<div>

    <div id="searchresults">Search results :</div>
    <ul id="results" class="update">
    </ul>

</div>
</div>

</body>
</html>

首先为您键入的输入字段创建html和jquery代码 然后使用ajax方法调用命中数据库的jquery函数keyup 然后创建一个管理搜索的php文件,我创建一个search.php文件

<?php
  $servername = "localhost";
  $username = "db_username";
  $password = "db_password";
  $dbname = "your_db_name";
  $searchquery = trim($_POST['search_text']); //input for search
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
  }

 $sql = "SELECT  filed1, field2 FROM yourtable_name WHERE match_text LIKE '%$searchquery%'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo " - Name: " . $row["filed1"]. " " . $row["field2"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

从此页面您将获得搜索结果,您可以根据需要进行更改。对于您的支票,如果您不搜索搜索文本长度&gt;,您还可以添加搜索文本长度。 2等等