自动完成返回“ 0个结果”

时间:2019-08-20 08:12:22

标签: php autocomplete

最近开始编码,第一次尝试使用准备好的语句,但返回的是“ 0结果”,我找不到错误。

自动完成功能没有准备好的语句,但是不知道我现在要去哪里。

<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName= "vlucht";

$mysqli = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

mysqli_set_charset($mysqli, 'utf8');

$id = $_GET['q'];
$disc = "%" . strtolower($id) . "%";
$sql = "SELECT DISTINCT postgemeente FROM overzicht WHERE LOWER (postgemeente) LIKE ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $disc);
$stmt->execute();

$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo $row["postgemeente"]. "\n";
    }
} else {
    echo "0 results";
}

$mysqli->close();
?>

1 个答案:

答案 0 :(得分:0)

如果您使用prepare-> bind-> execute .....,则需要删除此行

$result = $mysqli->query($sql);

查看评论

<?php
// help with debugging
ini_set('display_errors', 1); 
ini_set('log_errors',1); 
error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);


$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName= "vlucht";

$mysqli = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

mysqli_set_charset($mysqli, 'utf8');

$id = $_GET['q'];
$disc = "%" . strtolower($id) . "%";
$sql = "SELECT DISTINCT postgemeente FROM overzicht WHERE LOWER (postgemeente) LIKE ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $disc);
$stmt->execute();

// remove you are doing it differently now
//$result = $mysqli->query($sql);

// add to get a result object from a statement object
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo $row["postgemeente"]. "\n";
    }
} else {
    echo "0 results";
}

$mysqli->close();
?>