Auto complete not showing certain usernames

Basically, i am trynna make an autocomplete for usernames, it works when i enter my name "cool"

But whenever i enter my friends name that is also in the db named "vanilla" it doesn’t come up?

My HTML code:

<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Test</title>
<meta charset="utf-8">
<style>
.container{
    padding: 20px;
}
input{
    height: 25px;
    font-size: 16px;
}
</style>

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<!-- Initialize autocomplete -->
<script>
  $( function() {
    $( "#tags" ).autocomplete({
      source: 'search.php'
    });
  } );
  </script>
</head>
<body>
<div class="container"> 
        <div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>
</div>
</body>
</html>

search.php

<?php
// Database configuration
$dbHost     = "localhost"; 
$dbUsername = "username"; 
$dbPassword = "password"; 
$dbName     = "database"; 

// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

// Check connection
if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}

// Get search term
$searchTerm = $_GET['term'];

// Fetch matched data from the database
$query = $db->query("SELECT username FROM users WHERE username LIKE '%".$searchTerm."%'");

// Generate array with skills data
$skillData = array();
if($query->num_rows > 0){
    while($row = $query->fetch_assoc()){
        $data['id'] = $row['id'];
        $data['value'] = $row['username'];
        array_push($skillData, $data);
    }
}

// Return results as json encoded array
echo json_encode($skillData);
?>

I’ve looked around on google a little but it all gives the same answers and it doesn’t actually show my other users… except than only a selected few which is very strange.

So if anyone can point out the issue i’ll be forever greatful.

Regards.

102 thoughts on “Auto complete not showing certain usernames”

Leave a Comment