我创建了此代码来检索多个用户数据,其中密码=我通过post方法发布的密码,$value = 't'($value =列名(我想使用post方法发送列名(列名= a,b,ho,ll,c,d))我希望它查找"t")。 我的代码不完整,我不知道该怎么做,任何人都可以帮我解决这个问题吗?稍后我希望它与 android 应用程序连接并列出检索到的数据。
我在数据库操作中的函数.php
<?php
class DbOperations{
private $con;
function __construct(){
require_once dirname(__FILE__).'/DbConnect.php';
$db = new DbConnect();
$this->con = $db->connect();
}
public function gettestuser($pin){
$stmt = $this->con->prepare("SELECT * FROM test_category WHERE $value = 't' pin = ?");
$stmt->bind_param("s",$pin);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
我的获取测试用户.php
<?php
require_once '../include/DbOperations.php';
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){
if(isset($_POST['reg_pin'])){
$db = new DbOperations();
$test_category = $db->gettestuser($_POST['reg_pin']);
var_dump($test_category);
$response['error'] = false;
$response['pid'] = $test_category['pid'];
$response['name'] = $test_category['name'];
$response['pin'] = $test_category['pin'];
$response['a'] = $test_category['a'];
$response['b'] = $test_category['b'];
$response['ho'] = $test_category['ho'];
$response['ll'] = $test_category['ll'];
$response['c'] = $test_category['c'];
$response['d'] = $test_category['d'];
}else{
$response['error'] = true;
$response['message'] = "Required fields are missing";
}
}
echo json_encode($response);
?>
像这样的东西。
DbOperations.php:
public function gettestuser($col_name, $pin) {
$valid_columns = array('a' => 1, 'b' => 1, 'ho' => 1, 'll' => 1, 'c' => 1, 'd' => 1);
if (!array_key_exists($col_name, $valid_columns)) {
throw new Exception('Bad column name');
}
$stmt = $this->con->prepare("SELECT * FROM test_category WHERE $col_name = 't' pin = ?");
$stmt->bind_param("s", $pin);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
gettestuser.php:
<?php
require_once '../include/DbOperations.php';
$response = array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['reg_pin']) && isset($_POST['reg_value'])) {
$db = new DbOperations();
$test_category = $db->gettestuser($_POST['reg_value'], $_POST['reg_pin']);
var_dump($test_category);
$response['error'] = false;
$response['pid'] = $test_category['pid'];
$response['name'] = $test_category['name'];
$response['pin'] = $test_category['pin'];
$response['a'] = $test_category['a'];
$response['b'] = $test_category['b'];
$response['ho'] = $test_category['ho'];
$response['ll'] = $test_category['ll'];
$response['c'] = $test_category['c'];
$response['d'] = $test_category['d'];
} else {
$response['error'] = true;
$response['message'] = "Required fields are missing";
}
}
echo json_encode($response);
?>