2016-09-23 155 views
0

我找不到为什么我的php脚本总是返回NULL。我正在开发一个Android应用程序,通过返回JSON数据的PHP脚本从数据库获取用户数据。该应用程序解析数据,但我无法让PHP脚本返回NULL以外的任何内容。该应用使用JSON数据为用户返回的数据和result,这是1或0.我还是PHP新手,所以我有点困惑。从MSQL数据库返回数据的JSON总是返回NULL

这里是我的PHP脚本:

<?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 
} 

?> 
+1

可能的重复[我可以在PHP中混合MySQL API?](http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Chris

+1

请阅读你为什么[不应该使用'mysql_ *'函数](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 – Chris

+1

你在混合'mysql'和'mysqli'函数。他们不一样。你的连接是'mysqli',所以我会使用这些函数 – bassxzero

回答

0

您使用不当if-else格式,还应该对你的代码Fatal Error

} 

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 { 

即使支架上面只是这里的神器而不是在您的实际代码中,在if块{}和下一个else/elseif块{}的开头之间不能有任何内容。{}检查你的PHP错误日志 - 你应该有一个致命的错误。

另外,正如其他用户所述,您正在混合mysql()mysqli()函数,这是一个否定的。