gpt4 book ai didi

php - 使用 php mysql 进行实时搜索

转载 作者:行者123 更新时间:2023-11-29 07:22:12 25 4
gpt4 key购买 nike

我正在尝试使用 php html 和 mysql 开发一个实时搜索应用程序。当我运行它时,我收到错误

Failed to load resource: the server responded with a status of 404 (Not Found) error in server.

这是我的 html 和 php:

html:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
function getStates(value) {
$.post("mypage.php", {partialState: value}, function (data) {
$("#results").html(data);
});
}
</script>
</head>
<body>
<input type="text" onkeyup="getStates(this.value)" />
<br>
<div id="results"></div>
</body>
</html>

PHP:

<?php
mysql_connect("localhost:3306", "root", "root") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());
$partialStates = $_POST['partialState'];
$states = mysql_query("select name from software where name like '% {$partialStates}%'");
while ($state = mysql_fetch_array($states)) {
echo"<div>" . $states['name'] . "</div>";
}
?>

最佳答案

这适用于我的电脑。
html:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
function getStates(value){
$.post("mypage.php", {partialState:value}, function(data){
$("#results").html(data);
});
}
</script>
</head>
<body>
<input type="text" onkeyup="getStates(this.value)">
<br>
<div id="results"></div>
</body>
</html>

php:

<?php
$partialStates = $_POST['partialState'];
$query = 'SELECT name FROM software WHERE name LIKE "%'.$partialStates.'%"';
$mysqli = new mysqli("127.0.0.1","root","root","mydb");
if($mysqli->connect_errno){
echo "Keine Verbindung mit MySQL. :/";
}
$result = $mysqli->query($query);
$data = $result->fetch_all();
foreach($data as $row){
echo "<div>".$row[0]."</div>";
}
?>

如果仍然无法正常工作:
- 确保您的 mypage.php 和 mypage.html 位于同一目录中 -

关于php - 使用 php mysql 进行实时搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35741226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com