大家好,美好的一天,
我尝试使用 xampp 服务器连接到 phpmysql 的 android eclipse 项目。现在我成功获取并显示从mysql数据库到listview的所有数据。
但是如何只获取特定数据。示例:
名称:类别:代码主题:主题名称: 约翰福音 2 TBE124
约翰 2 TKE123
约翰 2 TZE125
约翰 2 TDE194
亚当2 TAE154
亚当2 TQE114
峡谷2 TGE164
峡谷2 TCE123
峡谷2 TBE126
我想要的是如果微调器选择 JOHN,ListView 将显示所有 JOHN 数据/行/列。如果选择 ADAM,则 ListView 将显示所有 ADAM 数据/行/列。对于我现在的代码,它将显示数据库中的所有数据。如果有相关教程请与我分享。
我使用了 androidhive 教程。 http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
//here the search value is what you send from the app
if(isset($_GET["matricID"])){
$string_input = $_GET['matricID'];
$result = mysql_query("SELECT * FROM tbl_semester WHERE matricID LIKE '%$matricID%'") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["tbl_semester"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$semester = array();
$semester["pid"] = $row["pid"];
$semester["matricID"] = $row["matricID"];
$semester["code"] = $row["code"];
$semester["course"] = $row["course"];
$semester["point"] = $row["point"];
$semester["crdhour"] = $row["crdhour"];
$semester["grdpoint"] = $row["grdpoint"];
$semester["reattempt"] = $row["reattempt"];
$semester["grd"] = $row["grd"];
// push single product into final response array
array_push($response["tbl_semester"], $semester);
}
// 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);
}
}
?>
最佳答案
这可以通过两种方式完成:
通过在每个微调器选择上查询 mysql 数据库。
- 每次选择微调器时都会向数据库发送请求。
- 服务器将返回一个 JSON 对象,解析该 json 对象并填充 ListView
- 通过这种方式,应用程序负载会减少,但服务器负载会增加一点。
仅处理 java 文件中的所有内容
- 制作一个与 key="name"对应的 JSON 对象:value={与该 name 相关的所有字段}
- 根据每个微调器选择的名称创建一个逻辑来查看此 JSON 对象,并使用与键名称对应的值字段填充 ListView
- 但这会向应用程序加载您可能不需要的数据,因为所有名称都将在 JSON 对象中加载其属性
我建议你实现第一种方法。
关于php - Android Eclipse 如何将php MySql数据库中的特定数据显示到 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25519019/