I am building an android application which uses mysql database as backend. I am trying to search through tables and multiple columns with a keyword and return them to the android app using php in a json format.
I tried a sample code i got from this question but it doesn't seem to work.
Here is my php code
<?php
require "conn.php";
$key = $_POST["key"];
$array = array();
$mysql_query = "(SELECT *, 'home' AS type FROM home WHERE food_name LIKE '%" . $key . "%' OR descrip LIKE '%" . $key ."%' OR user_name LIKE '%" . $key ."%'
UNION
SELECT *, 'c4c' AS type FROM cook4cash WHERE food_name LIKE '%" . $key . "%' OR descrip LIKE '%" . $key ."%' OR user_name LIKE '%" . $key ."%'
UNION
SELECT *, 'vendor' AS type FROM vendors_info WHERE user_name LIKE '%" . $key . "%'
UNION
SELECT *, 'customer' AS type FROM customers_info WHERE user_name LIKE '%" . $key . "%')";
mysql_query($query);
$result = mysqli_query($conn, $mysql_query);
if(mysqli_num_rows($result) > 0){
session_start();
while($row = mysqli_fetch_assoc($result)){
$array[] = $row;
//echo json_encode($row);
}
foreach($array as $new_array){
$new_array['id'] . '<br/>';
$new_array['food_name'] . '<br/>';
$new_array['descrip'] . '<br/>';
$new_array['price'] . '<br/>';
$new_array['quantity'] . '<br/>';
$new_array['image_url'] . '<br/>';
$new_array['user_name'] . '<br/>';
$new_array['profile_pic'] . '<br/>';
$new_array['delivery_time'] . '<br/>';
$new_array['delivery_cost'] . '<br/>';
$new_array['location'] . '<br/>';
$new_array['email'] . '<br/>';
}
echo json_encode(array("userInfo" => $array));
} else {
echo "No data was found";
}
?>
When i use postman to test, it gives the error code 500 (Internal Server Error). I want to know what is wrong with this code.