下面是我到目前为止出来的数据库连接类,但我会通过扩展PDO类本身进行改进,扩展PDO类
<?php
class database
{
protected $connection = null;
#make a connection
public function __construct($hostname,$dbname,$username,$password)
{
try
{
# MySQL with PDO_MYSQL
$this->connection = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
$this->connection = null;
die($e->getMessage());
}
}
#get the number of rows in a result
public function num_rows($query)
{
# create a prepared statement
$stmt = $this->connection->prepare($query);
if($stmt)
{
# execute query
$stmt->execute();
return $stmt->rowCount();
}
else
{
return self::get_error();
}
}
#display error
public function get_error()
{
$this->connection->errorInfo();
}
# closes the database connection when object is destroyed.
public function __destruct()
{
$this->connection = null;
}
}
?>
扩展类,
class database extends PDO
{
#make a connection
public function __construct($hostname,$dbname,$username,$password)
{
parent::__construct($hostname,$dbname,$username,$password);
try
{
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
die($e->getMessage());
}
}
#get the number of rows in a result
public function num_rows($query)
{
# create a prepared statement
$stmt = parent::prepare($query);
if($stmt)
{
# execute query
$stmt->execute();
return $stmt->rowCount();
}
else
{
return self::get_error();
}
}
#display error
public function get_error()
{
$this->connection->errorInfo();
}
# closes the database connection when object is destroyed.
public function __destruct()
{
$this->connection = null;
}
}
这是我如何实例化类,
# the host used to access DB
define('DB_HOST', 'localhost');
# the username used to access DB
define('DB_USER', 'root');
# the password for the username
define('DB_PASS', 'xxx');
# the name of your databse
define('DB_NAME', 'db_2011');
include 'class_database.php';
$connection = new database(DB_HOST,DB_NAME,DB_USER,DB_PASS);
$sql = "
SELECT *
FROM root_contacts_cfm
ORDER BY cnt_id DESC
";
$connection->num_rows($sql);
但我有错误,当我把这个扩展PDO类,
Warning: PDO::__construct() expects parameter 4 to be array, string given in C:\wamp\www\xx\class_database.php on line xx
Fatal error: Call to a member function setAttribute() on a non-object in C:\wamp\www\xx\class_database.php on line xx
我已经做了一些研究,在网上,我发现扩展PDO的这个基本结构,但我不明白它...
class myPDO extends PDO
{
public function __construct($dsn,
$username=null,
$password=null,
$driver_options=null)
{
parent::__construct($dsn, $username, $password, $driver_options);
}
public function query($query)
{
$result = parent::query($query);
// do other stuff you want to do here, then...
return($result);
}
}
什么是$ds
N个变量的呢?如何将我的$hostname
变量传递给扩展的pdo类?
另一个问题: 如何在扩展的pdo类中显示错误的方法? 如何关闭扩展的pdo类中的连接?
从mysqli移动到pdo是非常困难的!
谢谢。
看看你写的是什么,你真的不需要扩展PDO类。你只会让它比需要的更难。 – 2011-03-03 01:03:44
延长pdo是一件坏事?我以前有一个独立的mysqli类,但我被告知扩展mysqli更好,所以我认为它必须与pdo相同? – laukok 2011-03-03 01:10:34
你不需要重写构造函数btw。 – 2011-06-06 04:54:34