我只需要从Swift应用程序向SQL查询发送名称参数值。经过研究后,我发现我需要创建一个POST请求。
$ID = $_POST['customerName'];
$sql = "SELECT customer, rma, manufacturer, model, status FROM Equipment
WHERE customer = '$ID' ";
这是我目前在POST请求的PHP脚本中拥有的内容:
<?php
// Create connection
$con=mysqli_connect("localhost","username","password","dbName");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Equipment'
$ID = $_POST['customerName'];
$sql = "SELECT customer, rma, manufacturer, model, status FROM Equipment
WHERE customer = '$ID' ";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// Create temporary connection
$resultArray = array();
$tempArray = array();
// Look through each row
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
mysqli_close($con);
?>
然后,我需要我的Swift应用程序发送显示正确数据所需的customerName
值。下面,我尝试使用let参数:[String:Any] = [“ customerName”:“ Customer1”],没有结果。.
func downloadItems() {
let url = URL(string: "https://www.sample.com/test/test.php")!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let parameters: [String: Any] = ["customerName": "Customer1"]
request.httpBody = parameters.percentEscaped().data(using: .utf8)
let task = defaultSession.dataTask(with: url) { (data, response, error) in
if error != nil {
print("Error")
}else {
print("details downloaded")
self.parseJSON(data!)
}
}
task.resume()
}