gpt4 book ai didi

PHP 检查数据库中的项目

转载 作者:行者123 更新时间:2023-11-29 06:12:01 25 4
gpt4 key购买 nike

我是 PHP 新手,我正在尝试通过 MySQLi 连接检查 MySQL 数据库中是否存在用户。我尝试检查的用户名存储在名为 $code 的变量中。我应该在下面的语句中插入什么来让它检查 $code?

$stmt = $this->db->prepare("SELECT id_user FROM members WHERE username = ??? ($code)");  

感谢您的帮助。

编辑:这是我的代码:

class RedeemAPI {

private $db;

// Constructor - open DB connection
function __construct() {
$this->db = new mysqli('localhost:3306', 'username', 'password', 'db');
$this->db->autocommit(FALSE);
}

// Destructor - close DB connection
function __destruct() {
$this->db->close();
}

// Main method to redeem a code
function redeem() {

// Check for required parameters
if (isset($_POST["username"])) {

// Put parameters into local variables
$code = $_POST["username"];

// Look up code in database
$id_user= 0;

$stmt = $this->db->prepare('SELECT id_user FROM members WHERE username = ?');

$stmt->bind_param("s", $code);

$stmt->execute();

$stmt->bind_result($id_user);

while ($stmt->fetch()) {
break;
}
$stmt->close();

// Bail if code doesn't exist
if ($id_user <= 0) {
sendResponse(400, 'Invalid code');

return false;
}

// Return username, encoded with JSON
$result = array("username" => $code);
sendResponse(200, json_encode($result));
return true;
}
sendResponse(400, 'Invalid request');
return false;

}

}

最佳答案

参见 mysqli_stmt::bind_param() :

$stmt = $this->db->prepare("SELECT id_user FROM members WHERE username = ?");  
$stmt->bindParam('s', $code);
$stmt->exeucte();

关于PHP 检查数据库中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8637356/

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