我正在嘗試使用jQuery,Ajax和PHP爲MySQL表創建搜索引擎。我有一個數據庫在我的外部服務器上以獲取數據。這是MySQL數據庫表的圖片。
我對搜索引擎這兩個腳本:使用jQuery,Ajax和PHP不能正常工作搜索到MySQL表
的index.html
<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<input id="searchdb"/>
<div id="resultdb"></div>
<script type="text/javascript">
$(document).ready(function() {
$('#searchdb').change(function() {
$.ajax({
type: 'GET',
url: 'getdb.php',
data: 'ip=' + $('#searchdb').val(),
success: function(msg) {
$('#resultdb').html(msg);
}
});
});
});
</script>
</body>
</html>
getdb.php
<?php
if ($_GET['insert']) :
$insert = preg_replace('#[^0-9]#', '', $_GET['insert']);
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name FROM test WHERE id='.$insert.'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$row = $result->fetch_assoc()
echo $row["name"];
}
endif;
?>
我想是輸入ID,按回車鍵取回名稱或「0結果」。我寫的代碼似乎有些問題。你可以幫我嗎?提前致謝。
您正在使用'ip'創建查詢字符串,但您的PHP代碼查找'insert'。你的PHP代碼中的'$ row = $ result-> fetch_assoc()'後面還缺少一個分號。可能還有其他錯誤。 – 2015-07-21 02:33:49
可能會拉伸一些東西,稱之爲「搜索引擎」? – charlietfl
[**如何調試小程序** http://ericlippert.com/2014/03/05/how-to-debug-small-programs/](http://ericlippert.com/2014/03/05/how-to-debug-small-programs /) – spencer7593