PHP를 사용하여 my mysql 데이터베이스에서 검색 결과를 얻지 못했습니다. 나는 here에서 mysqli와 PDO 스타일을 사용해 보았다.jQuery 사용자가 검색 결과를 자동 완성하지 않습니다.
HTML
<div id="tags_wrapper">
<p>Tags</p>
<input type="text" class="txtTag" placeholder="Start entering tag.."> </input>
</div>
jQuery를 : 루트/블로그/panel.php에서 PHP 페이지에있는 루트/블로그/_class/tag_filler.php에있는 '소스'.
<script type="text/javascript">
$(document).ready(function(){
$('.txtTag').autocomplete(
{
source:'/_class/tag_filler.php',
minLength: 2
});
});
</script>
PHP 소스 [mysqli] : Brisktilities.php I는 아래에서 사용 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 소스 [PDO] : PDO 함께 여전히 검색 결과. 내 DB는 활발한이고 내 테이블은 입니다. 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 '용어는'/ _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
:
PDO ( http://php.net/manual/en/pdo.error-handling.php) 하십시오. MySQLi를 사용했습니다. 내가 위대한 튜토리얼에 갔던 장소는 http://youhack.me/2010/04/28/creating-a-fancy-search-feature-with-php-mysql-and-jquery/ 바로 다운로드하여 사용할 수 있습니다. , 나는 단순히 데이베이스를 광산으로 교체하고 코드를 약간 변경했습니다. 그러나 그것은 훌륭하게 작동하며 자동 완성됩니다. –
감사합니다 @ BenP.Dorsi-Todaro, 나는 그것에 대해 살펴보고 그것이 저에게 효과가 있는지 알아 봅니다. – user2690805
mysql 인젝션 엔진을 고칠 필요가 있습니다. –