0

I'm currently implementing a search engine with JQuery UI autocomplete. The records are stored in a mysql db. It returns results, but not enough. If I execute a search with LIKE %"abdo"% in phpmyadmin, I get 6 results; In the present case, with Jquery and the db call script, I don't even get one. Sometimes, it suggests the word, but only after 3 or 4 characters. It's surprising, because I put MinLength to 0 in the settings.

<script type="text/javascript">
    $(function() {
        //autocomplete
        $(".auto").autocomplete({
            source: "autocomplate.php",
            delay: 0,
            minLength: 0
        });

    });
</script>

And part of the autocomplate.php:

$stmt = $conn->prepare('SELECT word_fr FROM words_medina WHERE word_fr LIKE :term');
$stmt->execute(array('term' => '%' . $_GET['term'] . '%'));

while($row = $stmt->fetch()) {
    $return_arr[] =  $row['word_fr'];
}

Has anyboday any idea? I would appreciate your kind support. Thanks in advance!

Ramiz Wachtler
  • 5,623
  • 2
  • 28
  • 33
Laurent Voiry
  • 49
  • 1
  • 5
  • Looking for "acide" in the db, there are 16 matches. Again, not a single one with Jquery UI and the autocomplate query. – Laurent Voiry Dec 19 '17 at 20:01
  • Can you edit and supply an example of the response from `autocomplate.php?term=aci` or the like. Suspect the lack of JSON encoding might also play a part in this issue. – Twisty Dec 19 '17 at 22:08

1 Answers1

0

Would suggest using PHP like:

$stmt = $conn->prepare('SELECT word_fr FROM words_medina WHERE word_fr LIKE :term');
$stmt->execute(array('term' => '%' . $_GET['term'] . '%'));
while($row = $stmt->fetch()) {
    $return_arr[] =  $row['word_fr'];
}
header('Content-Type: application/json');
echo json_encode($return_arr);

See more: Returning JSON from a PHP Script

You should see results like:

[
  "string 1",
  "string 2",
  "string 3"
]

If you're not, then something else is an issue in your Query or PHP.

Twisty
  • 30,304
  • 2
  • 26
  • 45