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

How to Search value from input by mysqli in database(如何从数据库中的mysqli输入中搜索值)
本文介绍了如何从数据库中的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输入中搜索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)