0

我使用 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
4

1 回答 1

0

使用 like 时,您需要将值括在引号内。例如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();
}
于 2013-08-16T22:00:17.210 回答