php - 与网站、Swift + PHP 相比,应用程序中 mysql 查询的结果不同

标签 php mysql ios xcode swift

与仅使用 URL 获取结果相比,使用我的应用程序时,我从 MySQL 查询中获得了不同的结果。

以下内容访问数据库并显示包含 $_GET 值的任何用户名的用户名和 userID,通过 URL 访问时工作正常,但从我的应用程序调用时,它会显示来自的所有用户数据库。

服务器端:

<?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"}';
}
?>

应用程序端:

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)


}

}

最佳答案

这是因为您在应用中 POST 搜索查询,因此它是 post 变量。使用它来允许发布和获取:

$username = $_REQUEST['search'];

关于php - 与网站、Swift + PHP 相比,应用程序中 mysql 查询的结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25781959/

相关文章:

php - MySQL中的触发器语句出错

ios - 持续更新 UILabel 框架和文本

javascript - 如何在 iOS 中为 HTML、CSS 和 Javascript 页面制作包装器

php - 使用符合要求的php显示数据库中的数据

javascript - Laravel获取图像的文件内容但在生产环境上不起作用

php - jQuery移动ajax登录表单例份验证

ios - 在 iOS 8 模拟器上操作应用程序权限

php - PHP 样式有问题

php - 无法连接到数据库(ITS AUTO COMMENTING)

mysql - Django 模型创建时的竞争条件