我试图在其中一个php登录库中定义自己的函数。 定义了一些常量,它们完全正常,直到我自己调用databaseConnection()函数。
private function databaseConnection()
{
// if connection already exists
if ($this->db_connection != null) {
return true;
} else {
try {
// Generate a database connection, using the PDO connector
// @see http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
// Also important: We include the charset, as leaving it out seems to be a security issue:
// @see http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers#Connecting_to_MySQL says:
// "Adding the charset to the DSN is very important for security reasons,
// most examples you'll see around leave it out. MAKE SURE TO INCLUDE THE CHARSET!"
$this->db_connection = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME . ';charset=utf8', DB_USER, DB_PASS);
return true;
} catch (PDOException $e) {
$this->errors[] = MESSAGE_DATABASE_ERROR . $e->getMessage();
}
}
// default return
return false;
}
这是我定义的功能..
private function updateLastLoginDate($user_name)
{
if($this->databaseConnection())
{
$sth = $this->db_connection->prepare('UPDATE users '
. 'SET last_login_date = UTC_TIMESTAMP()'
. 'WHERE user_email =:user_name OR user_name =:user_name');
$sth->execute(array(':user_name' => $user_name));
}
}
当我调用这个函数时,错误表明databaseConntion函数中的所有常量都没有被定义..但除了我对我定义的函数的调用之外,它们工作得非常好...... 虽然我不喜欢pdo。