1. 程式人生 > >用php+MySQL實現檢索資料庫裡的內容。。。

用php+MySQL實現檢索資料庫裡的內容。。。

上程式碼:
– 將輸入的值作為模糊查詢的條件:
sql="SELECTFROMtestshopWHEREgoodsnameLIKE_GET[‘uname’].”%’”;

<?php
    $servername = "伺服器名";
    $username = "賬戶名";
    $password = "密碼";
    $dbname = "資料庫名";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"
>
<title>搜尋</title> <link rel = "stylesheet" type = "test/css" href = "sql_php.css"/> </head> <body> <form> <input type="text" id="txt_uname" name="uname"/> <br/> <input type="submit"/> <p> <?php
if (isset($_GET['uname'])) { //連上資料庫 $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } //查詢
$sql = "SELECT * FROM test_shop WHERE goods_name LIKE '%".$_GET['uname']."%'"; //執行 $result = $conn->query($sql); //輸出值 if ($result->num_rows > 0) { // 輸出每行資料 while($row = $result->fetch_assoc()) { echo '<p>' . $row['goods_name'] . '</p>'; } } else { echo "沒資料"; } //關閉資料庫連線,釋放資源 $conn->close(); } ?>
</p> </form> </body> </html>

看看效果。。。
這裡寫圖片描述