gpt4 book ai didi

php - 如何通过在 php mysql 中发布列名来搜索常量值?

转载 作者:行者123 更新时间:2023-11-29 02:43:16 25 4
gpt4 key购买 nike

我创建了这段代码来检索多个用户数据,其中 pincode = 我通过 post 方法发布的 pin-code 和 $value = 't'($value = 列名(我想使用发送列名post 方法(列名称= a、b、ho、ll、c、d))我希望它在其中查找“t”)。我的代码不完整,我不知道该怎么做,谁能帮我解决这个问题?稍后我希望它与 android 应用程序连接并列表查看检索到的数据。

我在 DbOperations.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();
}

我的gettestuser.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);
?>

关于php - 如何通过在 php mysql 中发布列名来搜索常量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46987445/

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