easy coder easy coder - 5 months ago 19
MySQL Question

Select or update single value using object oriented mysqli concept

I have these two methods which are used to set a flag in my

users
table.
I can see my
setEmailSent
function is working well, but
isEmailSent
always return 0, even its value is set to 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();
}


Can anyone tell me where I am wrong ???

Answer

You need to fetch data after bind_result as

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

Read bind_result

Comments