「已注销」 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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化