将一组 ID 从 Swift URLSession 传递到 PHP 以进行 SQL 查询?
Passing an array of ID's from Swift URLSession to PHP for an SQL query?
我有一个 iOS 应用程序,它有一个 ID 数组。这些 ID 都是数字:
var ids = [Int]() //Id array in my TableView
ids = basketStruct.getAllIds() //Pulling the array of ID's from my basket model.
print("This is how PHP sees the POST") //Debugging purposes
print(self.ids)
print("This is how PHP sees the POST")
basketServer.ids = self.ids //Passing the ID's to my URLSession.
URL会话按以下方式处理数组:
var ids = [Int]()
func downloadItems() {
request.httpMethod = "POST"
let postParameters = "ids="+String(describing: ids)
}
我已经不再发布 URLSession class 因为它可能没有必要。
现在我的XCode控制台如下:
This is how PHP sees the POST
[1,5,7,8]
This is how PHP sees the POST
Data downloaded
Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}
我没有返回任何结果。但是,如果我将所有 ID 数组更改为 NSArray 类型和 运行,只要我的数组仅包含 1 个 ID,我就会返回结果。第二次我向数组添加了 1 个以上的 ID,我得到了同样的错误。
现在,在我的 php 上,如果我将所有内容更改为 GET 而不是 POST 并直接在我的 URL 栏中输入一些值并转到浏览器中的页面,一切正常。我得到一个很好的 JSON 格式响应,其中包含我的 URL 栏中列出的所有 ID。
如果我将所有内容改回 POST,然后在尝试 运行 我的应用程序后访问我的 apache 错误日志文件,他们说如下:
[Thu Feb 22 20:51:42.873663 2018] [:error] [pid 18793] [client 192.168.1.46:60790] PHP Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[1])' at line 1 in /var/www/api/DbOperation.php:92\nStack trace:\n#0 /var/www/api/DbOperation.php(92): PDO->prepare('SELECT * FROM `...')\n#1 /var/www/api/Basket.php(14): DbOperation->basket('[1, 5, 7, 8]')\n#2 {main}\n thrown in /var/www/api/DbOperation.php on line 92
我知道这个问题很广泛,由很多部分组成,但我被卡住了:(非常感谢任何帮助。
谢谢
P.S - 如果有人想看,这是我的 PHP 代码:
这是 PHP 页面,我的 URL 会话也进行了:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once dirname(__FILE__) . '/DbOperation.php';
$ids = $_POST["ids"];
$db = new DbOperation();
$json = $db->basket($ids);
}
这里是 DbOperation:
public function basket($ids) {
require dirname(__FILE__) . '/../../dbconnect.php';
$sql = "SELECT * FROM `Menu` WHERE `ID` IN ($ids)";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
echo json_encode($result);
}
在 PHP 异常输出中,您可以看到篮子函数是使用如下字符串调用的:
DbOperation->basket('[1, 5, 7, 8]')
因此,PHP 代码将生成此 SQL:
$sql = "SELECT * FROM `Menu` WHERE `ID` IN ($ids)";
即:
SELECT * FROM `Menu` WHERE `ID` IN ([1, 5, 7, 8])
这是无效的SQL。
您可以通过将 $ids 字符串转换为适用于 SQL 的格式来实现此目的。
// Remove '[' and ']' characters
$inValues = str_replace(['[',']'], '', $ids);
$sql = "SELECT * FROM `Menu` WHERE `ID` IN ($inValues)";
现在您将获得:
SELECT * FROM `Menu` WHERE `ID` IN (1, 5, 7, 8)
我有一个 iOS 应用程序,它有一个 ID 数组。这些 ID 都是数字:
var ids = [Int]() //Id array in my TableView
ids = basketStruct.getAllIds() //Pulling the array of ID's from my basket model.
print("This is how PHP sees the POST") //Debugging purposes
print(self.ids)
print("This is how PHP sees the POST")
basketServer.ids = self.ids //Passing the ID's to my URLSession.
URL会话按以下方式处理数组:
var ids = [Int]()
func downloadItems() {
request.httpMethod = "POST"
let postParameters = "ids="+String(describing: ids)
}
我已经不再发布 URLSession class 因为它可能没有必要。
现在我的XCode控制台如下:
This is how PHP sees the POST
[1,5,7,8]
This is how PHP sees the POST
Data downloaded
Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}
我没有返回任何结果。但是,如果我将所有 ID 数组更改为 NSArray 类型和 运行,只要我的数组仅包含 1 个 ID,我就会返回结果。第二次我向数组添加了 1 个以上的 ID,我得到了同样的错误。
现在,在我的 php 上,如果我将所有内容更改为 GET 而不是 POST 并直接在我的 URL 栏中输入一些值并转到浏览器中的页面,一切正常。我得到一个很好的 JSON 格式响应,其中包含我的 URL 栏中列出的所有 ID。
如果我将所有内容改回 POST,然后在尝试 运行 我的应用程序后访问我的 apache 错误日志文件,他们说如下:
[Thu Feb 22 20:51:42.873663 2018] [:error] [pid 18793] [client 192.168.1.46:60790] PHP Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[1])' at line 1 in /var/www/api/DbOperation.php:92\nStack trace:\n#0 /var/www/api/DbOperation.php(92): PDO->prepare('SELECT * FROM `...')\n#1 /var/www/api/Basket.php(14): DbOperation->basket('[1, 5, 7, 8]')\n#2 {main}\n thrown in /var/www/api/DbOperation.php on line 92
我知道这个问题很广泛,由很多部分组成,但我被卡住了:(非常感谢任何帮助。
谢谢
P.S - 如果有人想看,这是我的 PHP 代码:
这是 PHP 页面,我的 URL 会话也进行了:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once dirname(__FILE__) . '/DbOperation.php';
$ids = $_POST["ids"];
$db = new DbOperation();
$json = $db->basket($ids);
}
这里是 DbOperation:
public function basket($ids) {
require dirname(__FILE__) . '/../../dbconnect.php';
$sql = "SELECT * FROM `Menu` WHERE `ID` IN ($ids)";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
echo json_encode($result);
}
在 PHP 异常输出中,您可以看到篮子函数是使用如下字符串调用的:
DbOperation->basket('[1, 5, 7, 8]')
因此,PHP 代码将生成此 SQL:
$sql = "SELECT * FROM `Menu` WHERE `ID` IN ($ids)";
即:
SELECT * FROM `Menu` WHERE `ID` IN ([1, 5, 7, 8])
这是无效的SQL。
您可以通过将 $ids 字符串转换为适用于 SQL 的格式来实现此目的。
// Remove '[' and ']' characters
$inValues = str_replace(['[',']'], '', $ids);
$sql = "SELECT * FROM `Menu` WHERE `ID` IN ($inValues)";
现在您将获得:
SELECT * FROM `Menu` WHERE `ID` IN (1, 5, 7, 8)