gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:48:30 25 4
gpt4 key购买 nike

与仅使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com