我使用php从我的mysql数据库中找不到搜索结果。我尝试过使用here中的mysqli和PDO样式。
HTML
<div id="tags_wrapper">
<p>Tags</p>
<input type="text" class="txtTag" placeholder="Start entering tag.."> </input>
</div>
jQuery :位于root / Blog / panel.php的php页面,位于root / Blog / _class / tag_filler.php中的“source”。
<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
答案 0 :(得分:0)
使用时,您需要将值括在引号内。例如tValue like '%my_value%'
。
请注意以下单引号的使用(我只显示需要更新的行):
在 mysqli :
中$mysqli->query("SELECT * FROM Tag_T WHERE tValue LIKE '%".$search."%' ORDER BY tValue ASC")
并在您的 PDO :
中$ac_term = "'%".$_GET['term']."%'";
<强>更新强>
如果您已尝试过此操作,请添加一些错误处理,以便缩小问题根源。
对于 mysqli (http://php.net/manual/en/mysqli.error.php):
$queryTags = $mysqli->query("SELECT * FROM Tag_T WHERE tValue LIKE %".$search."% ORDER BY tValue ASC");
if (!$queryTags) {
printf("Error: %s", mysqli_error($mysqli));
}
while($row = mysqli_fetch_array($queryTags)) {
$results[] = array('id' => $row['tID'], 'label' => $row['tValue']);
}
echo json_encode($results);
PDO ( http://php.net/manual/en/pdo.error-handling.php):
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage();
die();
}