扩展 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 类时出现错误,
警告:PDO::__construct() 需要 参数4为数组,给定字符串 在 C:\wamp\www\xx\class_database.php 上线xx线
致命错误:调用成员函数 setAttribute() 在非对象上 C:\wamp\www\xx\class_database.php 上 第 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 太难了!
谢谢。
Below is the db connection class I came out with so far, but I am going to improve it by extending the PDO class itself,
<?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;
}
}
?>
extended class,
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;
}
}
This is how I instantiate the class,
# 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);
But I have errors when I call this extended pdo class,
Warning: PDO::__construct() expects
parameter 4 to be array, string given
in C:\wamp\www\xx\class_database.php
on line xxFatal error: Call to a member function
setAttribute() on a non-object in
C:\wamp\www\xx\class_database.php on
line xx
I have done some research online, I found this basic structure of extending pdo but I dont understand it...
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);
}
}
What is $ds
n variable for? How can I pass my $hostname
variable into extended pdo class?
Another questions:
How can I make a method for displaying error in the extended pdo class?
How can I close the connection in the extended pdo class?
It is so difficult to move from mysqli to pdo!
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$dsn 是数据源名称。它为您处理您的主机名。您可以像这样使用它:
通过行
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
您已经设置了发生错误时引发的异常(我喜欢),因此,在扩展类中,您可以处理异常处理程序中的错误。如果您的扩展 PDO 类中有一个名为 getAssoc 的方法,那么它将如下所示:$dsn is data source name. It handles your hostname for you. You use it like this:
With the line
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
You have set exceptions to be raised when errors occur (which I like), so in your extended class you can handle errors in exception handlers. If you had a method called getAssoc in your extended PDO class then it would look like this:我会专注于类需要做什么,而不是尝试重写 PDO。我有同样的想法,因为我虽然它会简化和简化代码,但事实并非如此。您最终会花费更多时间来研究如何间接与 PDO 交互。
I would concentrate on what the class needs to do rather than trying to re write the PDO. I had the same idea because i though it would simplify and stream line the code but it doesn't. You end up spending more time working out how to indirectly interface with the PDO.