I'm sending the "formKATID" parameter to PHP. I want to send more than one "formKATID". For example, I want to select the data in the database where KATEGORI.ID = 6,7,8. That's why I have defined more than one formKATID in bodyObject. But it doesn't work that way. How can I submit more than one "formKATID"? I print the "ExistCount" row with json_encode and try to extract the count output by swift, but it comes with a null value. There is a problem with the Array I sent.
PHP
...
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>$databaseName);
$conn = sqlsrv_connect($serverName, $connectionInfo);
$formKATID = $_POST['formKATID'];
$tsql = "SELECT ... IN(".implode(', ',$formKATID).")";
$stmt = sqlsrv_query( $conn, $tsql);
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$paketCount = $row['ExistCount'];
$results[] = Array("Count" => $paketCount);
}
echo json_encode($results, JSON_UNESCAPED_UNICODE);
sqlsrv_free_stmt( $stmt);
?>
...
SWIFT
@objc func kategoriSaydır(){
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.httpMethod = "POST"
let bodyObject = ["formKATID": ["6", "7", "8"]] as [String : Any]
request.httpBody = try! JSONSerialization.data(withJSONObject: bodyObject, options: [])
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
// error
return
}
do {
if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for review in baslik {
if let soru_baslik = review["Count"] as? String {
let s = String(describing: soru_baslik)
print("kategoriItemCount", s)
}
}
}
} catch let parseError { ... }
}
task.resume()
}
task.resume()?