gpt4 book ai didi

php - 如何从 MySQL 数据库中检索选定的行并将信息发送回 Android?

转载 作者:搜寻专家 更新时间:2023-11-01 08:45:01 24 4
gpt4 key购买 nike

我正在做一个 Android 项目,我对 Android 完全陌生。我正在学习以下教程 link .我的项目是关于警察登记系统的。我在 PHP 和 JSON 的帮助下连接到 MySQL 数据库。

我已经创建了一个表单,用户可以在其中注册,他们可以使用他们的用户名和密码登录。我还制作了一个表格,用户可以在其中注册他们的投诉,投诉存储在数据库中称为 db_complaints 的单个数据库中。我可以使用以下 PHP 代码检索所有投诉。

<?php

/*
Our "config.inc.php" file connects to database every time we include or require
it within a php script. Since we want this script to add a new user to our db,
we will be talking with our database, and therefore,
let's require the connection to happen:
*/
require("config.inc.php");

//initial query

if (!empty($_POST)) {
//initial query
$query = "INSERT INTO complaints ( username, title, message ) VALUES ( :user, :title, :message ) ";

//Update query
$query_params = array(
':user' => $_POST['username'],
':title' => $_POST['title'],
':message' => $_POST['message']
);



//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}

// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();


if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();

foreach ($rows as $row) {
$post = array();
$post["post_id"] = $row["post_id"];

$post["username"] = $row["username"];
$post["title"] = $row["title"];
$post["message"] = $row["message"];


//update our repsonse JSON data
array_push($response["posts"], $post);
}

// echoing JSON response
echo json_encode($response);


} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}}

?>

我想单独检索特定的用户投诉,以便他们可以在 my_complaints 表单中查看投诉,但我无法做到这一点,我一直被卡住了。

最佳答案

使用准备好的语句和错误处理,您的代码看起来非常整洁,那么您的问题实际上在哪里?

$query = "SELECT * FROM complaints WHERE username = :user";
$query_params = array(':user' => $_POST['username']);

该查询应该可以用来检索特定用户的投诉。

关于php - 如何从 MySQL 数据库中检索选定的行并将信息发送回 Android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29298633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com