Is the method used below to get data from a database efficient and optimal? The data is in a MySQL database, and in my server I have a PHP file with the following code to return some information:
if($_POST["method"] == "requestBusinessFood") {
requestBusinessFood();
}
function requestBusinessFood() {
$categoryID = $_POST["category"];
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'fooddatabase';
$conn = mysqli_connect($host, $user, $pass, $db);
$sql = "SELECT * FROM `foodtablebusiness` WHERE category = " . $categoryID;
$result = mysqli_query($conn, $sql);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
echo json_encode($rows);
}
On the webpage, I have a js file to retrieve the information in the following way:
function createAJAXRequestToPopulateList(category) {
return $.ajax({ url: '../server.php',
data: {method: 'requestBusinessFood',
category: category},
type: 'post'
});
}
function addActivityItem(){
var selector = document.getElementById("categorySelector");
ajaxRequest = createAJAXRequestToPopulateList(selector.options[selector.selectedIndex].value);
ajaxRequest.done(populateList);
}
function populateList(responseData) {
console.log(responseData);
}