0

I can't figure out why my php script always returns NULL. I'm developing an Android app that gets user data from a database via a PHP script that returns JSON data. The app parses the data, but I can't get the PHP script to return anything other than NULL. The app uses JSON data for both the data returned for the user, and the result, which is either 1 or 0. I'm still new to PHP, so I'm a bit confused.

Here's my PHP script:

<?php
define('HOST','localhost');
define('USER','user');
define('PASS','password');
define('DB','database');

if (!empty($_POST)){

    if (empty($_POST['userID'])){
        $response["success"] = 0; 
        $response["message"] = "Please enter a User ID";
        die(json_encode($response));
    }

$userID = mysql_escape_string($_POST['userID']); 

$con = mysqli_connect(HOST,USER,PASS,DB);



$result = mysql_query("SELECT* FROM users WHERE id = $userID");


if ($result && mysql_num_rows($result) > 0){

    $response["result"] = array(); 

    while($row = mysql_fetch_array($result)){
        $finduser = array(); 
        $finduser["id"] = $row["id"]; 
        $finduser["firstname"] = $row["firstname"]; 
        $finduser["company"] = $row["company"]; 
        $finduser["position"] = $row["position"]; 

        array_push($response["result"], $finduser);     
    }

    $response["success"] = 1; 
}

echo json_encode($response); 




} else {
?>
        <h1>Search by User ID:</h1> 
        <form action="searchbyuserid.php" method="post"> 
            Enter the UserID of the receipient:<br /> 
            <input type="text" name="userID" placeholder="User ID" /> 
            <br /><br /> 
            <input type="submit" value="Submit" /> 
        </form> 
        <a href="register.php">Register</a>
    <?php
}

?> 
Kevin Bright
  • 961
  • 2
  • 9
  • 17
  • 1
    Possible duplicate of [Can I mix MySQL APIs in PHP?](http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – ChrisGPT was on strike Sep 23 '16 at 18:45
  • 1
    Please read about why you [shouldn't be using `mysql_*` functions anymore](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – ChrisGPT was on strike Sep 23 '16 at 18:49
  • 1
    You're mixing `mysql` and `mysqli` functions. They are not the same. You're connection is a `mysqli` so i would use those functions – bassxzero Sep 23 '16 at 18:55
  • `The app uses JSON data for both the data returned for the user, and the result` ??? What do you mean? Does not make sense. – greenapps Sep 23 '16 at 19:17

1 Answers1

0

You're using improper if-else format and also should have a Fatal Error with your code:

}

echo json_encode($response); // don't put anything between the close of the if and the beginning of the next block

//the next closing brace (or the previous one) shouldn't be here
} else {

Even if the brace above is just an artifact here and not in your actual code, you can't have anything between the end of the if block {} and the start of the next else/elseif block {}. Check your PHP error log - you should have a fatal error.

Also, as other users have stated, you are mixing mysql() and mysqli() functions, which is a no-no.