1
我正在使用PHP Web服務從MYSQL數據庫獲取數據的解決方案。目前,它正在爲查詢傳遞單個數據。PHP MySQL服務從多個ID獲取數據
現在我正在尋找在搜索查詢中使用多個ID,所以例如我想要拉回一個基於多個ID的產品列表。我已經把我目前的PHP服務放在了下面,我試圖有效地修改它以做我需要的東西,但是不能到達那裏。
<?php
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/dbConnect.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["id"])) {
$id = $_GET['id'];
// get all products from products table
$result = mysql_query("SELECT * FROM products WHERE id = '$id'") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// venues node
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product= array();
$product["id"] = $row["id"];
$product["name"] = $row["name"];
$product["type"] = $row["type"];
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
}
?>
此代碼的工作,我傳遞一個ID爲PHP,這讓我的單品回來了,不過,我希望能夠通過多個ID對PHP的數組中,也許再有這些ID的用於查詢。我已經嘗試了下面的解決方案,但似乎無法讓它工作,我也明白這個PHP不是最好的,特別是對於防止SQL注入,所以任何其他建議,這是值得歡迎的。
WHERE id IN (' . implode(',', $ids) . ')';
在此先感謝
保護注射
你能告訴我IDS – 2013-04-28 10:01:53
首先,我想$數組值你在'WHERE ID IN ...'之前缺少一個''''。其次,爲什麼解決方案沒有工作?你檢查了正在生成的查詢嗎?因爲使用像這樣的'implode'或'join',應該可以工作... – Vapire 2013-04-28 10:01:53
該數組將是[2,50,236 ...],並且完全查詢行是$ result = mysql_query(「SELECT * FROM products WHERE id IN('。implode(',',$ ids)。')「或die(mysql_error()); – user723858 2013-04-28 10:08:10