2017-03-06 81 views
-5

我想在使用類型的值的第一個字符時顯示我的數據庫值。 讓我來描述它與我的網站 我有一個網站的主頁輸入用戶類型輸入爲列車號。我需要那個用戶類型的火車號。他從我存儲的數據庫中獲得了火車名。如何在數據庫中通過mysqli輸入搜索值

+3

你試過嗎?如果是的請提供你的代碼 –

+0

我已經在我的網站上試過[列車運行狀態](https://trainsrunningstatus.net) –

回答

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> 

首先創建輸入字段,鍵入 然後使用AJAX方法 然後創建一個PHP文件,管理自己的搜索我創建的search.php文件調用jQuery函數KEYUP擊中數據庫,HTML和jQuery代碼

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

從這個頁面你會得到你的搜索結果,你可以改變它作爲你的需求。爲您的檢查,你也可以添加搜索文本的長度,如果你不搜索,如果搜索文本長度> 2或等

+0

謝謝shafiqul-islam –

+0

@ M.joe你可以放棄投票嗎? –

0

我並沒有真正得到您的問題,但我得到了什麼,你可以使用這個查詢:

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

+0

謝謝@ roun512 –

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

希望你能理解這是如何工作

+0

謝謝@ oke-tega –

相關問題