「已注销」 2017-05-10 13:51
浏览 63
已采纳

从Swift检索数据到PHP然后回到Swift [重复]

In my PHP file, I have this code:

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

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

I check it in HTML and it works. Nothing's wrong with it. However, when I try to put it in Swift, I have a problem with that. I don't how will I put value of "user_id" from Swift file to PHP file then go back to Swift to execute the PHP file.

Here's my Swift code:

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()
                                    })
                                }
                            }
                        }

I already tried this Swift code and it also works. I have a default value for "user_id", it is 0 and I just add some info about it just for me to see if my Swift code works and it is.

</div>
  • 写回答

1条回答 默认 最新

  • dougai6464 2017-05-10 14:30
    关注

    Swift 3.1

    In PHP

    By your requirement there are different options available. As well as above code the same way on connect.php. But you need to write SELECT query to INSERT query for send parameters through mobile end and Also you need one more reference variable to get whether the method is INSERT or SELECT

    For example,

    Just consider you are get value from URL like https://somesite.somedomain/databaseoperations.php and your databaseoperations.php has following queries

    $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"]);
    }
    

    In iOS

    Just perform URLRequest with post parameter with user_id and operation.

    For get Value your post parameters like below,

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

    For Put Value postParam becomes,

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

    For reference :-

    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()
    

    I hope it'll be give some idea to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?