I have a sql table with products and i don't get how to retrieve several products using array with their ids
i know how retrieve all of these products and only one using id
JSON with all products from sql
function loadGoods(){
$conn = connect();
$sql = "SELECT * FROM PRODUCTS";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$out = array();
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$out[$row["id"]] = $row;
}
echo json_encode($out);
} else {
echo 0;
}
mysqli_close($conn);
}
JSON with one product using its id received in js file from hash
function loadSingleGoods(){
$id = $_POST['id'];
$conn = connect();
$sql = "SELECT * FROM PRODUCTS WHERE id='$id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
echo json_encode($row);
} else {
echo 0;
}
}
Both of these snippets are working and now i have array with id's but i don't get how retrieve only their properties from sql in JSON file.
I have array with ids like this ["6", "7", "27"] and i need to retrieve from sql PRODUCTS only items with these ids. As i understand i have to do something with this line
$sql = "SELECT * FROM PRODUCTS WHERE id='$id'";
i tried such code
$sql = "SELECT * FROM PRODUCTS WHERE in_array("id", $ids, TRUE);
but it didn't work