Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added database class
Source Link
cakeman
  • 85
  • 1
  • 7

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

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); } } 
improved formatting and explained what I thought the code should do
Source Link
cakeman
  • 85
  • 1
  • 7
 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>";

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

Source Link
cakeman
  • 85
  • 1
  • 7

UPDATE with rowCount

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