将数据从Swift检索到PHP然后再回到Swift

时间:2017-05-10 13:51:04

标签: php swift

在我的PHP文件中,我有这段代码:

$user_id = $_POST['user_id']; 
require_once('Connect.php'); 

$sql = "SELECT * FROM Loans WHERE user_id='$user_id'"; 

我用HTML检查它是否有效。没有什么不对的。但是,当我尝试将其放入Swift时,我遇到了问题。我不知道如何设置" user_id"从Swift文件到PHP文件,然后返回Swift执行PHP文件。

这是我的Swift代码:

if let loanArray = jsonObj!.value(forKey: "loans")  as? NSArray
                        {
                            for loans in loanArray
                            {
                                if let loanDict = loans as? NSDictionary
                                {
                                    if let name = loanDict.value(forKey: getBnum)
                                    {
                                        self.borrNoArray.append(name as! String)
                                    }

                                    if let name = loanDict.value(forKey: "loan_no")
                                    {
                                        self.loanNoArray.append(name as! String)
                                    }

                                    if let name = loanDict.value(forKey: "amount")
                                    {
                                        self.loanAmtArray.append(name as! String)
                                    }

                                    OperationQueue.main.addOperation({
                                        self.tableView.reloadData()
                                    })
                                }
                            }
                        }

我已经尝试过这个Swift代码,它也可以运行。我有" user_id"的默认值,它是0,我只是添加一些关于它的信息,只是为了让我看看我的Swift代码是否有效。

1 个答案:

答案 0 :(得分:0)

Swift 3.1

在PHP中

根据您的要求,有不同的选择。以上代码与connect.php上的代码相同。但是,您需要通过移动端向SELECT查询发送INSERT parameters查询。此外,您还需要一个参考变量来获取该方法是INSERT还是{{ 1}}

例如,

只要考虑您从SELECT这样的网址中获取价值,并且您的https://somesite.somedomain/databaseoperations.php有以下查询

databaseoperations.php

在iOS

只需使用$user_id = $_POST['user_id']; $operation = $_POST['operation']; require_once('Connect.php'); if($operation == "select"){ $sql = "SELECT * FROM Loans WHERE user_id=?"; .... // Here, perform Select from database using $user_id as a // bound param, then echo json_encode() }else if ($operation == "insert"){ //Just perform Database Insert operation }else{ echo json_encode(["error"=>"Illegal Operation"]); } URLRequest post参数user_id执行operation

为获取值,您的帖子参数如下,

let url = "https://somsite.somedomain/databaseoperations.php"
let postParam = "user_id=4&operation=select"

对于Put Value postParam变为,

let postParam = "user_id=5&operation=insert"

供参考: -

let postParam="user_id=5&operation=insert"
let url = URL(string: "https://somsite.somedomain/databaseoperations.php")
var request = URLRequest(url: url!)
request.httpBody = postParam.data(using: String.Encoding.utf8)
request.httpMethod = "POST"
let task = URLSession.shared.dataTask(with: request) { data,response,error in
        guard error == nil else{
            print(error!)
            return
        }

    do{
        let jsonObject = try JSONSerialization.jsonObject(with: data!, options: .allowFragments)
        print(jsonObject)
    }catch{

        print(error.localizedDescription)
    }
}
task.resume()

我希望能给你一些想法。