gpt4 book ai didi

php - PHP 中的 MySQL 访问类

转载 作者:行者123 更新时间:2023-11-29 09:19:25 26 4
gpt4 key购买 nike

我有一个 MySQL 连接类,如下所示:

class MySQLConnect
{
private $connection;
private static $instances = 0;

function __construct()
{
if(MySQLConnect::$instances == 0)
{
//Connect to MySQL server
$this->connection = mysql_connect(MySQLConfig::HOST, MySQLConfig::USER, MySQLConfig::PASS)
or die("Error: Unable to connect to the MySQL Server.");
MySQLConnect::$instances = 1;
}
else
{
$msg = "Close the existing instance of the MySQLConnector class.";
die($msg);
}
}

public function singleQuery($query, $databasename)
{
mysql_select_db(MySQLConfig::DB, $this->connection)
or die("Error: Could not select database " . MySQLConfig::DB . " from the server.");
$result = mysql_query($query) or die('Query failed.');
return $result;
}

public function createResultSet($query, $databasename)
{
$rs = new MySQLResultSet($query, MySQLConfig::DB, $this->connection ) ;
return $rs;
}

public function close()
{
MySQLConnect::$instances = 0;
if(isset($this->connection) ) {
mysql_close($this->connection) ;
unset($this->connection) ;
}
}

public function __destruct()
{
$this->close();
}
}

MySQLResultSet 类如下所示:

class MySQLResultSet implements Iterator
{
private $query;
private $databasename;
private $connection;
private $result;

private $currentRow;
private $key = 0;
private $valid;

public function __construct($query, $databasename, $connection)
{
$this->query = $query;
//Select the database
$selectedDatabase = mysql_select_db($databasename, $connection)
or die("Error: Could not select database " . $this->dbname . " from the server.");
$this->result = mysql_query($this->query) or die('Query failed.');
$this->rewind();
}

public function getResult()
{
return $this->result;
}

// public function getRow()
// {
// return mysql_fetch_row($this->result);
// }

public function getNumberRows()
{
return mysql_num_rows($this->result);
}

//current() returns the current row
public function current()
{
return $this->currentRow;
}

//key() returns the current index
public function key()
{
return $this->key;
}

//next() moves forward one index
public function next()
{
if($this->currentRow = mysql_fetch_array($this->result) ) {
$this->valid = true;
$this->key++;
}else{
$this->valid = false;
}
}

//rewind() moves to the starting index
public function rewind()
{
$this->key = 0;
if(mysql_num_rows($this->result) > 0)
{
if(mysql_data_seek($this->result, 0) )
{
$this->valid = true;
$this->key = 0;
$this->currentRow = mysql_fetch_array($this->result);
}
}
else
{
$this->valid = false;
}
}

//valid returns 1 if the current position is a valid array index
//and 0 if it is not valid
public function valid()
{
return $this->valid;
}
}

以下类是我如何访问数据库的示例:

class ImageCount
{
public function getCount()
{
$mysqlConnector = new MySQLConnect();
$query = "SELECT * FROM images;";
$resultSet = $mysqlConnector->createResultSet($query, MySQLConfig::DB);
$mysqlConnector->close();
return $resultSet->getNumberRows();
}
}

我像这样使用 ImageCount 类:

if(!ImageCount::getCount())
{
//Do something
}

问题:这是访问数据库的好方法吗?如果效果不好,有人可以推荐一种替代方法吗?

谢谢。

最佳答案

嘿,Mike,实现您自己的类来处理数据库连接没有任何问题,到目前为止您所拥有的都很好,但是 PHP 已经提供了一个用于处理数据库连接的接口(interface),无论您连接到哪个数据库管理器。我建议你看一下http://www.php.net/manual/en/book.pdo.php因为它几乎具有处理查询、语句、结果集、错误等所需的所有功能。

干杯,米。

关于php - PHP 中的 MySQL 访问类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2726030/

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