我是编程方面的新手,我的英语不太好。我的项目遇到了问题。我有String ArrayList,它只包含一些数字作为String。使用以下代码,我将其转换为Gson。
String finalRequestedList = new Gson().toJson(listOfWordsId);///// is my string arraylist///
我用齐射将其发送到服务器。它已将类似的内容发送到服务器:
["1080","16","17","18","2154","151","0","3","1762","6","127","7","91","70","72","20"]
我尝试通过以下行接收此数据
$content = $_POST['wordsList'];
现在,我想知道如何使用$ content来运行这样的查询:
$query="SELECT newG.dId,newG.dWord ,newE.word FROM newG LEFT JOIN newE ON newG.dId =
newE.id where newG.dId= 1080,16,17,18,...(all received numbers);
我的代码的最后一部分是这样的:
$result=$connection->query($query);
$rows = array();
while ($assoc_row = $result->fetch(PDO::FETCH_ASSOC)){
$rows[] = $assoc_row;
}
print json_encode($rows);
}catch(PDOExceptoin $e){
echo $e;
}
这里描述:https://phpdelusions.net/pdo#in
应用于您的代码,它应该看起来像这样:
$content = $_POST['wordsList'];
$in = str_repeat('?,', count($content) - 1) . '?';
$sql = "SELECT newG.dId,newG.dWord ,newE.word
FROM newG
LEFT JOIN newE ON newG.dId = newE.id
WHERE newG.dId IN ($in)";
$stm = $db->prepare($sql);
$stm->execute($content);
$rows = $stm->fetchAll();
print json_encode($rows);
不需要循环,可以使用fetchAll()
。