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

时间:2016-06-06 08:32:36

标签: php mysql mysqli

我有两个用于在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();
    }

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

1 个答案:

答案 0 :(得分:1)

您需要在bind_result之后获取数据

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

阅读bind_result