我用齐射向服务器发送了一个字符串列表。如何在php中读取此数据并对其运行查询?

问题描述 投票:0回答:1

我是编程方面的新手,我的英语不太好。我的项目遇到了问题。我有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;
}
php android mysql pdo android-volley
1个回答
0
投票

这里描述: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()

© www.soinside.com 2019 - 2024. All rights reserved.