如何从数据库中的mysqli输入中搜索值

时间:2023-03-05
本文介绍了如何从数据库中的mysqli输入中搜索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

解决方案

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

<头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>PHP、jQuery搜索演示</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 () {$("输入").keyup(function () {$('#results').html('');var searchString = $("#search_box").val();var data = 'search_text=' + searchString;如果(搜索字符串){$.ajax({类型:POST",网址:'search.php',数据:数据,数据类型:'文本',异步:假,缓存:假,成功:功能(结果){$('#results').html(result);//window.location.reload();}});}});});<身体><div id="容器"><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/></表单>

<div><div id="searchresults">搜索结果:</div><ul id="results" class="update">

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

connect_error){die("连接失败:" . $conn->connect_error);}$sql = "从 yourtable_name WHERE match_text LIKE '%$searchquery%' 中选择字段 1,字段 2";$result = $conn->query($sql);如果($result->num_rows>0){//输出每一行的数据while($row = $result->fetch_assoc()) {回声-名称:".$row["filed1"]."".$row["field2"]."<br>";}} 别的 {echo "0 结果";}$conn->close();?>

从此页面您将获得搜索结果,您可以根据需要进行更改.为了您的检查,您还可以添加搜索文本长度,如果您不搜索,如果搜索文本长度 > 2 或 etc

I want to show my database value when use type first character of the value. Let me describe it with my site I have a website with homepage input where user type input as train no. I need that user type train no. he got train name from my database where i store.

解决方案

check this code .i think it will help you

<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>

first create html and jquery code for input field which you type then call jquery function keyup which hit database using ajax method then create a php file which manage your search i create a search.php file

<?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();
?>

from this page you will get your search result and you can change it as your demand . for your check you can also add search text length if you do not search if search text length > 2 or etc

这篇关于如何从数据库中的mysqli输入中搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:PHP:为什么我不能在 mysqli_fetch_array() 结果上循环两次? 下一篇:通过php将html表单数据发送到sql数据库(使用mysqli)

相关文章