我使用 php 从我的 mysql 数据库中没有得到任何搜索结果。我已经尝试使用 mysqli 和此处的 PDO 样式。
html
<div id="tags_wrapper">
<p>Tags</p>
<input type="text" class="txtTag" placeholder="Start entering tag.."> </input>
</div>
jQuery:位于根/Blog/panel.php 的 php 页面中,“源”位于根/Blog/_class/tag_filler.php。
<script type="text/javascript">
$(document).ready(function(){
$('.txtTag').autocomplete(
{
source:'/_class/tag_filler.php',
minLength: 2
});
});
</script>
php source [mysqli] : Brisktilities.php 创建了一个我在下面使用的 mysqli 实例。
include_once 'BriskUtilities.php';
$util = new BriskUtilities();
$mysqli = $util->getMysqli();
if(isset($_GET['term'])) {
$search = $_GET['term'];
if($queryTags = $mysqli->query("SELECT * FROM Tag_T WHERE tValue LIKE %".$search."% ORDER BY tValue ASC")) {
while($row = mysqli_fetch_array($queryTags)) {
$results[] = array('id' => $row['tID'], 'label' => $row['tValue']);
}
echo json_encode($results);
}
$mysqli->close();
}
php source [PDO] : 仍然没有 PDO 的搜索结果。我的数据库很活跃,我的表是tag_t,并且我的连接工作正常。有什么建议么?
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass) or die ("<p class='error'>Sorry, we were unable to connect to the database server.</p>");
}
catch(PDOException $e) {
echo $e->getMessage();
}
$return_arr = array();
if ($conn)
{
$ac_term = "%".$_GET['term']."%";
$query = "SELECT * FROM tag_t where tValue like :term";
$result = $conn->prepare($query);
$result->bindValue(":term",$ac_term);
$result->execute();
/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$row_array['id'] = $row['id'];
$row_array['value'] = $row['tValue'];
array_push($return_arr,$row_array);
}
}
$conn = null;
echo json_encode($return_arr);
未找到更新 GET 'term'(将源更改为 Blog/_class/tag_filler.php 时错误消失,但仍然没有返回搜索结果。我在同一页面上使用数据库,它正在为另一个表获取结果):
Failed to load resource: the server responded with a status of 404 (Not Found) [http] site.local/_class/tag_filler.php?term=an
Failed to load resource: the server responded with a status of 404 (Not Found) site.local/_class/tag_filler.php?term=and
GET site.local/_class/tag_filler.php?term=my 404 (Not Found) jquery-1.9.1.js:8526