I have a database named (myDatabase) and a table inside which is called Category, inside the table I have 3 columns (ids, categories, product_name), I want to retrieve the values or rows that contains specific categories, or let’s say fetching values inside the table depending on an array containing some categories available in the table. This is my code but it gives error, it is not working when I want to print them.
$conn = mysqli_connect('localhost','root','','myDatabase');
$MyCategories = array('food','entertaintment','vehicle');
$where_in = implode(',', $MyCategories);
$query = "SELECT * FROM Category WHERE categories IN ($MyCategories);";
$result = mysqli_query($conn,$query);
$cats = mysqli_fetch_all($result,MYSQLI_ASSOC);
print_r($cats);
I tried that with mysqli_fetch_array() but it doesn’t work, says (arrays to string conversion) error.
$conn = mysqli_connect('localhost', 'root', '', 'myDatabase');
$MyCategories = array('food', 'entertaintment', 'vehicle');
$where_in = implode(',', $MyCategories);
$query = "SELECT * FROM Category WHERE categories IN ($MyCategories);";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)) {
echo $row['2'];
// third column (product_name) in table Category
}
please help me, thanks a bunch.