When using Select2 on localhost (XAMPP) server to get data from the database, it works perfectly. But after deploying it to server, it doesn't work, and console shows 500 Internal Server Error.
Following is the code I'm using:
$('#search-box').select2({
placeholder: 'Select an item',
ajax: {
url: 'php/search.php',
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
});
And the PHP code in search.php:
require_once("dbinfo.php");
// Opens a connection to a MySQL server
$connection=mysqli_connect ('localhost', $username, $password);
if (!$connection) { die('Not connected : ' . mysqli_error($connection));}
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error($connection));
}
$sql = "SELECT c_id, city_name FROM city
WHERE city_name LIKE '%".$_GET['q']."%'
LIMIT 10";
$result = $connection->query($sql);
$json = [];
while($row = $result->fetch_assoc()){
$json[] = ['id'=>$row['c_id'], 'text'=>$row['city_name']];
}
echo json_encode($json);
What is going wrong here?