我无法从php文件中获取数据。 我正在制作像myfitnesspal这样的健康和健身类型的应用程序。 我无法在php文件中输出查询的reuslt并在swift中打印结果。
这是php(getDetails.php)
<?php
require("Conn.php");
require("MySQLDao.php");
$email = htmlentities($_POST["email"]);
$returnValue = array();
$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetails($email);
$returnValue["user"] = $userDetails;
echo json_encode($returnValue);
$dao->closeConnection();
?>
MySQLDao.php
public function getUserDetails($email)
{
$returnValue = array();
$test = array();
$sql = "select calories from users where username='" . $email . "'";
$result = $this->conn->query($sql);
if ($result != null && (mysqli_num_rows($result) >= 1)) {
$row = $result->fetch_array(MYSQLI_ASSOC);
$returnValue = $row;
if (!empty($row)) {
}
}
return $returnValue;
}
最后是我的快速代码
let myURL = NSURL(string: "http://localhost:8888/getDetails.php");
let request = NSMutableURLRequest(URL:myURL!);
request.HTTPMethod = "POST";
let postString = "email=\(userEmail)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
if let parseJSON = json{
let resultValue = parseJSON["user"] as! String!;
print("results:\(resultValue)")
}
}
catch {print(error)}
}
task.resume();