首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用rowCount更新

用rowCount更新
EN

Stack Overflow用户
提问于 2017-09-19 22:03:39
回答 1查看 52关注 0票数 0

基本上,我试图显示一条显示消息,这取决于数据库中的行是否被更改,我四处查看并建议使用rowCount,但它仍然显示了错误的输出。

代码语言:javascript
复制
function makeActive()
{
    try{
        $database = new Database;
        $text = "";
        $activecode = $_GET["activecode"];

        $setActive = "UPDATE username SET active = '1', activecode = 'Active' WHERE activecode = :activecode";              
        $stmt = $database->query($setActive);
        $database->bind(':activecode', $activecode);
        $database->execute();
        $update = $database->rowCount();

        if ($update === 1)
        {
            return $text .="Your account is now active" . "<br><a href='index.php'>Home page</a>";
        }
        else
        {
            return $text .="Your account is already active" . "<br><a href='index.php'>Home page</a>";              
        }

        }
        catch(Exception $e ) 
        {
            return $text .= "Something went wrong contact site administrator" . "<br><a href='index.php'>Login page</a>";
            header( "refresh:10; url=index.php" ); 
        }
}

因此,基本的情况是,如果一个行被更新,它应该显示return $text .="Your account is now active" . "<br><a href='index.php'>Home page</a>";

添加数据库类

代码语言:javascript
复制
class Database
{
    private $host = "localhost";
    private $user = "root";
    private $pass = "";
    private $dbname = "got";

    private $dbh;
    private $error;
    private $stmt;

    public function __construct()
    {
        //SET DSN
        $dsn = "mysql:host=". $this->host . ";dbname=" . $this->dbname;

        $options = array
        (
            PDO::ATTR_PERSISTENT    => true,
            PDO::ATTR_ERRMODE       =>PDO::ERRMODE_EXCEPTION
        );
    //create PDO
    try
    {
        $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
    }
    catch(PDOEception $e)
    {
        $this->error = $e->getMessage();
    }
    }

    public function query($query)
    {
        $this->stmt = $this->dbh->prepare($query);
    }

    public function bind($param, $value, $type = null)
    {
        if(is_null($type))
        {
            switch(true)
            {
                case is_int($value):
                $type = PDO::PARAM_INT;
                break;
                case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
                case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
                default:
                $type = PDO::PARAM_STR;
            }
        }

        $this->stmt->bindValue($param, $value, $type);
    }

    public function execute()
    {
        return $this->stmt->execute();
    }

    public function lastInsertId()
    {
        $this->dbh->lastInsertId();
    }
    public function rowCount()
    {
        $this->stmt->rowCount();
    }

    public function connect()
    {
        $this->dbh->connect();
    }

    public function resultset()
    {
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-20 01:32:20

这就是问题所在:

取代该方法:

代码语言:javascript
复制
public function rowCount()
{
    $this->stmt->rowCount();
}

在这方面:

代码语言:javascript
复制
public function rowCount()
{
    return $this->stmt->rowCount();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46310372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档