与网站,Swift + PHP相比,app中mysql查询的结果不同

时间:2014-09-11 07:49:11

标签: php mysql ios xcode swift

与使用网址获取结果相比,我在使用自己的应用时获得的MySQL查询结果不同。

以下访问数据库并显示包含$_GET值的任何用户名的用户名和用户ID,在通过URL访问时工作正常,但从我的应用程序调用时,它会显示数据库中的所有用户。 / p>

服务器端:

<?php
require("lib.php");
header('Content-type: application/json');
$username = $_GET['search'];
$search = "%$username%";
$result = query("SELECT username, userID FROM user_accounts WHERE username LIKE '%s'", $search);
if (!$result['error']) {
    if (count($result['result'])>0) {
        class Emp {
            public $success = "";
            public $result = "";
        }
        $e = new Emp();
        $e->success = 1;
        $e->result = $result['result'];
        echo json_encode($e);

    } else {
        echo '{"success":0,"error_message":"No users found"}';
    }
} else {
    echo '{"success":0,"error_message":"Connection error"}';
}
?>

App方面:

protocol SearchUsersAPIProtocol {
    func didFindUsers(results: NSDictionary)
}

class SearchUsersAPI: NSObject {

var data: NSMutableData = NSMutableData()


var delegate: SearchUsersAPIProtocol?

func searchUser(search: String) {
    println("search string is \(search)")
    var post = NSString(string: "search=\(search)")
    var urlPath: String = "URL"

    var postData = post.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    var postLength = NSString(format: "%lu", postData!.length)

    var url: NSURL = NSURL(string: urlPath)
    var request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.setValue(postLength, forHTTPHeaderField: "Content-Length")
    request.setValue("application/json", forHTTPHeaderField: "Accept")
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.HTTPBody = postData


    var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)

    connection.start()
}


func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
    println("Connection failed.\(error.localizedDescription)")
}


func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
    // Recieved a new request, clear out the data object
    println("connection did receive")
    self.data = NSMutableData()
}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
    // Append the recieved chunk of data to our data object
    println("connection")
    self.data.appendData(data)

}

func connectionDidFinishLoading(connection: NSURLConnection!) {
    // Request complete, self.data should now hold the resulting info
    // Convert the retrieved data in to an object through JSON deserialization
    println("connection finished")
    var err: NSError
    var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
    // Now send the JSON result to our delegate object
    println("results: \(jsonResult)")
    delegate?.didFindUsers(jsonResult)


}

}

1 个答案:

答案 0 :(得分:0)

因为在你的应用中你POST搜索查询,所以它是变量。使用此选项允许发布和获取:

$username = $_REQUEST['search'];