【问题标题】:UPDATE with rowCount使用 rowCount 更新
【发布时间】:2017-09-19 22:03:39
【问题描述】:

基本上,我试图根据数据库中的行是否更改来显示一条显示消息,我环顾四周,建议使用rowCount,我这样做了,但它仍然向我显示输出错误

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" . "&lt;br&gt;&lt;a href='index.php'&gt;Home page&lt;/a&gt;";

添加数据库类

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);
    }
}

【问题讨论】:

    标签: php pdo


    【解决方案1】:

    这就是问题所在:

    替换方法:

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

    用这个:

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

    【讨论】:

    • 啊,你是明星,我可以睡觉了,现在是 02:35,谢谢 :)
    • 不客气。很高兴能帮助到你。对我来说是一小时后 :-) Cakeman,对于你的项目,如果它对你有帮助的话:my db adapter class。它在答案的“编辑”部分。再见。
    • 我醒来时会看,我已经在书上标明了,否则我根本睡不着lol
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多