2013-08-16 62 views
0

我没有从我的mysql数据库使用php的搜索结果。我尝试过使用mysqli和here的PDO风格。jQuery ui自动完成没有搜索结果

HTML

<div id="tags_wrapper"> 
    <p>Tags</p> 
    <input type="text" class="txtTag" placeholder="Start entering tag..">  </input> 
</div> 

的jQuery:位于PHP页面在根/博客/ panel.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创建的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没有搜索结果。我的数据库是轻快,我的表是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 
+0

我最近正在与jQuery和自动完成的搜索引擎。它使用MySQLi。我去了一个很好的教程的地方是http://youhack.me/2010/04/28/creating-a-fancy-search-feature-with-php-mysql-and-jquery/下载工作开箱即用,我简单地用我的代码替换了dabase,并对代码做了一些小改动。但它工作的很好,而且它是自动完成的。 –

+0

感谢@ BenP.Dorsi-Todaro,我会看看它,看看它是否适用于我 – user2690805

+0

值得一提的是,你从mysql注入修复引擎。 –

回答

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); 

对于PDOhttp://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(); 
} 
+0

@ user2690805,建议在你的mysqli和pdo实现中添加一些错误处理来调试问题。请参阅更新中的相应链接。 – vee

+0

更新:我注意到在控制台中,我在'term'上获得了404的GET – user2690805

+0

在您的文档根目录中是否存在'_check/tag_filter.php'? – vee

相关问题