gpt4 book ai didi

php - 使用面向对象的 mysqli 概念选择或更新单个值

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

我有这两种方法,用于在我的 users 表中设置标志。我可以看到我的 setEmailSent 函数运行良好,但 isEmailSent 始终返回 0,即使其值设置为 1>.

class Mydatabase{
function connect(){
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
ini_set("error_log","/log/php_error_log");
error_reporting(E_ALL | E_STRICT);
error_reporting('1');
$connect_error = 'Sorry we are experiencing down time.';
$host = "some.com";
$username = "username";
$password ='password';

$db = "loginregister";
$mysqli = new mysqli($host, $username, $password, $db) or die($connect_error);
return $mysqli;
}

}
function isEmailSent($username){

//**********************************************************************************
// @Desc - will check if email already sent to the user
// @Parms - str username
// @return - details int
//**********************************************************************************

//set up database connection
$db = new Mydatabase();
$mysqli = $db->connect();

//sql statement
$sql = "SELECT `emailSent` FROM `users` WHERE `username` = ?";

//sql prepare statement
$stmt= $mysqli->prepare($sql);

//bind sql params
$stmt->bind_param('s', $username);

//sql execute statement
$stmt->execute();

//store sql result
$stmt->store_result();

//bind sql result
$stmt->bind_result($emailSent);
return $emailSent;
}


function setEmailSent($userName){

//**********************************************************************************
// @Desc - will set that email already sent to the user
// @Parms - str username
//**********************************************************************************

//set up database connection
$db = new Mydatabase();
$mysqli = $db->connect();

$stmt = $mysqli->prepare("UPDATE `users` SET `emailSent`=? WHERE `username`=?");

if ($stmt === false) {
trigger_error($mysqli->error, E_USER_ERROR);
}

$stmt->bind_param('is', $emailSent,$userName);

$emailSent = 1;

$stmt->execute();
}

谁能告诉我哪里错了???

最佳答案

您需要在bind_result之后获取数据为

$stmt->bind_result($emailSent);
$stmt->fetch();// fetch data
return $emailSent;// then return

阅读bind_result

关于php - 使用面向对象的 mysqli 概念选择或更新单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37652839/

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