0
基本上我試圖顯示一個顯示消息,取決於數據庫中的行是否被改變,我環顧四周,並建議使用我所做的rowCount
,但它仍然顯示我錯誤的輸出更新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");
}
}
所以basicaly應該發生的是,如果一個行被更新,它應該顯示哪些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);
}
}
啊,你是個明星,我可以去睡覺了它的02:35這裏 謝謝:) – cakeman
不客氣。很高興幫助你。對我來說它是一個小時後:-) Cakeman,爲您的項目,如果它可以幫助你:[我的數據庫適配器類](https://stackoverflow.com/questions/46014772/return-multiple-response-data-in-one - 響應/ 46018999#46018999)。它在答案的「編輯」部分。再見。 –
我會看起來,當我醒來時,我已預訂標記,否則我不會睡覺大聲笑 – cakeman