-1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Pistis
  • 1
  • 2
  • Here is a very good example how SO is hurting PHP community. The question you linked to is 8 years old. The answers there are only for historical purposes and will not work in modern PHP. Furthermore, they are vulnerable to SQL injection. – Dharman Aug 20 '19 at 11:04
  • May I know what does this line mean, mysql_query($query); just after raw query? – Muhammed Shihabudeen Labba A Aug 20 '19 at 11:09
  • 1
    @MuhammedShihabudeenLabbaA I think that was the line causing the internal server error. I took it out and now it returns status code 200. Thank you very much. – Pistis Aug 20 '19 at 11:21
  • Worth a look for comparison... https://stackoverflow.com/q/23428273/2943403 – mickmackusa Aug 20 '19 at 11:26

1 Answers1

1

Did you check PHP error_logs? I guess reason of the problem is line 14. You're using mysqli_* methods but on line 14, you had tried to use a mysql_* method. I think you should remove the code on this line and try again.

izniburak
  • 246
  • 3
  • 4