0

Basically I'm trying to show a display message depending on if the row within a database was changed or not, I've looked around and it was suggested to use rowCount which I did, however it's still showing me the wrong output

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

So basicaly what should happen is if a row is updated, it should display return $text .="Your account is now active" . "<br><a href='index.php'>Home page</a>";

Added database class

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);
    }
}
cakeman
  • 85
  • 7

1 Answers1

1

Here it is the problem:

Replace the method:

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

with this:

public function rowCount()
{
    return $this->stmt->rowCount();
}
  • Ahh, you're a star and I can go to bed it's 02:35 here Thank you :) – cakeman Sep 20 '17 at 01:36
  • 1
    You are welcome. Glad to have helped you. By me it's one hour later :-) Cakeman, for your project, if it helps you: [my db adapter class](https://stackoverflow.com/questions/46014772/return-multiple-response-data-in-one-response/46018999#46018999). It's in the "EDIT" part of the answer. Bye. –  Sep 20 '17 at 01:38
  • I will look when i wake up, I've book marked it otherwise I won't sleep at all lol – cakeman Sep 20 '17 at 01:41