我正在创建一个 Android 应用程序,但又到达了没有桥梁的 Android 河,所以我在这里 build 桥梁并跨越。我正在尝试在 Android 应用程序上实现评论系统,但它将来自拍卖/竞价系统,但我想使用评论系统来解决它。
我有一个名为 comment 的数据库,我希望 Android 应用程序从中获取值并显示,但我希望应用程序获取具有相同 comment_id 示例的所有值。
id comment_id comment
1 1 love this product
2 1 not bad
3 2 too expensive
4 2 how much
5 2 too old
6 3 not that new
7 5 hate it
8 7 go away
9 8 what wrong with you
10 4 hi
11 7 hi
12 4 helo
13 7 hola
14 4 go away
15 6 nice
16 3 bye
我问如何实现此功能,以便在使用相同的 comment_id 调用时显示所有值。
这是我的完整 PHP 脚本,也许它可以很好地指导我们:
if($_SERVER['REQUEST_METHOD']=='POST'){
include_once($_SERVER['DOCUMENT_ROOT']."/config/config.php");
$id= $_POST['id'];
// Create connection
$conn = new mysqli($host, $user, $pass, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM product where id = '$id'" ;
$result = $conn->query($sql);
if ($result->num_rows >0) {
while($row[] = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
}
?>
我不确定出了什么问题;该脚本获取并显示所有值。我希望它只显示与 comment_id 中相同的值。
最佳答案
Cursor cursor = db.query("product",
null,
"comment_id = ?",
new String[]{YOUR_ID_VARIABLE_HERE},
null, null, null);
if (cursor != null)
cursor.moveToFirst();
Integer id = Integer.parseInt(cursor.getString(0));
String comment = cursor.getString(2);
关于php - 获取具有相同值同表的SQL数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46505489/