2017-09-19 23 views
0

基本的には、データベース内の行が変更されたかどうかによって表示メッセージを表示しようとしています。私は見回して、rowCountを使用するように提案されましたが、間違った出力だから、basicaly行が更新された場合、それが表示されるはずされてどうするかUPDATE with 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" . "<br><a href='index.php'>Home page</a>";

追加のデータベースクラス

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

答えて

1

ここではそれが問題である。これに

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

は、メソッドを置き換え

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

ああ、あなたがスターだと私はそれがここに午前2時35分のベッドに行くことができ ありがとうございました:) – cakeman

+1

ようこそ。あなたを助けてくれてうれしいです。私の場合は1時間後です:-) Cakeman、あなたのプロジェクトのために、あなたが助けてくれれば:[my db adapter class](https://stackoverflow.com/questions/46014772/return-multiple-response-data-in-one -response/46018999#46018999)。答えの「編集」部分にあります。さようなら。 –

+0

私は目を覚ますときに見ます、それ以外の場合は私は本をマークしました私はまったく寝ません – cakeman