Database Structure and Goal
I have a MySQL table called customers with this structure:
insert into customers (customer_id, fname, lname, email_address, nation) values (1, 'Sarah', 'Johnson', '[email protected]', 'Canada');
I need to filter records to show only specific countries like “Germany”, “Brazil”, “Australia”, etc.
Current PHP Backend Code
database.php (connection file)
<?php
$hostname = "localhost";
$username = "root";
$pass = "root";
$database = "myDatabase";
$connection = mysqli_connect($hostname, $username, $pass, $database);
if (!$connection) {
die("Database connection error: " . mysqli_connect_error());
}
?>
api.php (data retrieval)
<?php
include "database.php";
$whereClause = "1";
if(isset($_GET['customer_id'])){
$customerId = mysqli_real_escape_string($connection, trim($_GET['customer_id']));
$whereClause = " customer_id=" . mysqli_real_escape_string($connection, $_GET['customer_id']);
}
$customerData = mysqli_query($connection, "select * from customers WHERE " . $whereClause);
$results = array();
while($record = mysqli_fetch_assoc($customerData)){
$results[] = $record;
}
echo json_encode($results);
exit;
?>
Vue.js Frontend
var application = new Vue({
el: '#application',
data: {
customers: "",
customerId: 0
},
methods: {
fetchAllData: function(){
axios.get('api.php')
.then(function (result) {
application.customers = result.data;
})
.catch(function (err) {
console.log(err);
});
},
fetchById: function(){
if(this.customerId > 0){
axios.get('api.php', {
params: {
customerId: this.customerId
}
})
.then(function (result) {
application.customers = result.data;
})
.catch(function (err) {
console.log(err);
});
}
}
}
});
How can I modify this setup to filter by multiple specific countries instead of fetching all records?