gpt4 book ai didi

php - 扩展 PDO 类

转载 作者:IT王子 更新时间:2023-10-29 00:28:05 26 4
gpt4 key购买 nike

下面是我到目前为止提出的数据库连接类,但我将通过扩展 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);
}
}

什么是 $dsn 变量?如何将我的 $hostname 变量传递给扩展的 pdo 类?

其他问题:如何在扩展的 pdo 类中创建显示错误的方法?如何关闭扩展 pdo 类中的连接?

从mysqli迁移到pdo好难!

谢谢。

最佳答案

$dsn 是数据源名称。它为您处理您的主机名。你可以这样使用它:

$dsn = 'mysql:dbname=YOUR_DB_NAME;host=YOUR_HOSTNAME'

使用 $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 行您已设置发生错误时引发的异常(我喜欢),因此在您的扩展类,您可以处理异常处理程序中的错误。如果你的扩展 PDO 类中有一个名为 getAssoc 的方法,那么它看起来像这样:

/// Get an associative array of results for the sql.
public function getAssoc($sql, $params=array())
{
try
{
$stmt = $this->prepare($sql);
$params = is_array($params) ? $params : array($params);
$stmt->execute($params);

return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch (Exception $e)
{
// Echo the error or Re-throw it to catch it higher up where you have more
// information on where it occurred in your program.
// e.g echo 'Error: ' . $e->getMessage();

throw new Exception(
__METHOD__ . 'Exception Raised for sql: ' . var_export($sql, true) .
' Params: ' . var_export($params, true) .
' Error_Info: ' . var_export($this->errorInfo(), true),
0,
$e);
}
}

关于php - 扩展 PDO 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5175357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com