Php 数据库连接类建议
我创建这个类是为了连接到我的数据库,并在某个地方读到,拥有不同的用户进行连接是最安全的。因此,我为我目前使用的四个不同选项创建了一个用户,其中一个用于更新、选择、删除和插入。我真的不确定是否有必要为其中的每一个创建一个用户。我基本上在想我能做些什么来改进这门课。我知道这些问题在这里出现过很多次,但每个人的课堂似乎都不一样,所以我想我应该问一下 这是代码的粘贴箱。。它很长,否则我就把它贴在这里。如果pastebin是一个问题,我还是会添加代码 (ninetwozero编辑:将代码内联)Php 数据库连接类建议,php,Php,我创建这个类是为了连接到我的数据库,并在某个地方读到,拥有不同的用户进行连接是最安全的。因此,我为我目前使用的四个不同选项创建了一个用户,其中一个用于更新、选择、删除和插入。我真的不确定是否有必要为其中的每一个创建一个用户。我基本上在想我能做些什么来改进这门课。我知道这些问题在这里出现过很多次,但每个人的课堂似乎都不一样,所以我想我应该问一下 这是代码的粘贴箱。。它很长,否则我就把它贴在这里。如果pastebin是一个问题,我还是会添加代码 (ninetwozero编辑:将代码内联) 你可能
你可能没有正确理解事物。大多数情况下,数据库访问应该在事务内部进行,这是有保证的。在同一事务中,您将有选择、插入、更新和删除。每种操作有4个不同的用户(因此有4个独立的连接,因此有4个独立的事务),这是一个非常糟糕的主意。您可能还没有正确理解事情。大多数情况下,数据库访问应该在事务内部进行,这是有保证的。在同一事务中,您将有选择、插入、更新和删除。每种操作有4个不同的用户(因此有4个独立的连接,因此有4个独立的事务)是一个非常糟糕的主意。某个用户可以执行的操作限制不应该委托给代码,而是由数据库服务器本身通过其权限管理来管理。即使使用非常(太???)粗粒度的权限模式,它也会很快变得非常混乱,正如您的代码所证明的那样。现在假设您需要在每个表的基础上管理这些权限
正如其他人已经说过的,我将坚持使用现有的数据库连接解决方案,并学习如何最大限度地利用数据库的权限系统,例如,让仅显示数据的应用程序作为具有“刚读”权限的用户运行,等等。对某个用户可以执行的操作的限制不应委托给代码,而是由数据库服务器本身通过其权限管理进行管理。即使使用非常(太???)粗粒度的权限模式,它也会很快变得非常混乱,正如您的代码所证明的那样。现在假设您需要在每个表的基础上管理这些权限
正如其他人已经说过的,我将坚持使用现有的数据库连接解决方案,并学习如何最大限度地利用数据库的权限系统,例如,让仅显示数据的应用程序作为具有“刚读”权限的用户运行,等等。如果您要创建不同的数据库用户,那么这些用户应该针对使用您的应用程序的不同类型(和级别)的用户,而不是针对数据库上的每个权限,因为单个用户可能会希望在某个时候执行上述操作。。。但只在某些桌子上。因此,您可以为来宾、经过身份验证的用户、管理员等创建不同的用户。例如,来宾将无法更新(甚至无法选择?)user_profiles表
正如我在评论中提到的。。。您不应该将db连接参数存储在类本身中。应该从安全位置(可能在web根目录上方)读入这些数据库,这样,如果PHP受到破坏,您的数据库就不会受到影响。如果您要创建不同的数据库用户,那么这些数据库用户应该针对使用您的应用程序的不同类型(和级别)的用户,而不是针对数据库上的每个权限,由于单个用户可能希望在某个时候完成上述所有操作。。。但只在某些桌子上。因此,您可以为来宾、经过身份验证的用户、管理员等创建不同的用户。例如,来宾将无法更新(甚至无法选择?)user_profiles表
正如我在评论中提到的。。。您不应该将db连接参数存储在类本身中。应该从一个安全的位置(可能在web根目录上方)读入这些文件,这样,如果PHP受到破坏,您的数据库就不会受到影响。我建议停止为已经创建的更好的文件创建自己的库。有很多连接类的解决方案,我使用(不再开发)捷克数据库层dibi:这些乱七八糟的东西到底是为了什么???我创建自己的类的主要原因是为了更好地理解和学习php、mysql和所有这些。我应该看看其他创建的类,看看它们都做了些什么。至于混乱,它在ZS中看起来不像一团混乱,但在pastebin上它搞乱了格式。@Pavel我不认为它是一个库-只是一个db连接类,尽管它相当冗长。你不应该将db连接参数存储在类本身中。应该从一个安全的位置(可能在web根目录之上)读入这些文件,这样,如果PHP受到破坏,您的数据库就不会受到影响。我建议停止为已经创建的更好的文件创建您自己的库。有很多连接类的解决方案,我使用(不再开发)捷克数据库层dibi:这些乱七八糟的东西到底是为了什么???我创建自己的类的主要原因是为了更好地理解和学习php、mysql和所有这些。我应该看看其他创建的类,看看它们都做了些什么。至于混乱,它在ZS中看起来不像一团混乱,但在pastebin上它搞乱了格式。@Pavel我不认为它是一个库-只是一个db连接类,尽管它相当冗长。你不应该将db连接参数存储在类本身中。应该从一个安全的位置(可能在web根目录之上)读取这些数据,这样,如果PHP受到破坏,您的数据库就不会受到影响。您是对的,我似乎误解了整个数据库事务,但这就是我为什么要这样做的原因
<?php
class DB_Connection {
//Subject to change
protected $_DATABASE = '#';
protected $_HOST = '#';
protected $_SELECT = array( 'connection' => null,
'user' => '#',
'pass' => '#',
'alive' => FALSE,
'thread' => '' );
protected $_INSERT = array( 'connection' => null,
'user' => '#',
'pass' => '#',
'alive' => FALSE,
'thread' => '' );
protected $_DELETE = array( 'connection' => null,
'user' => '#',
'pass' => '#',
'alive' => FALSE,
'thread' => '' );
protected $_UPDATE = array( 'connection' => null,
'user' => '#',
'pass' => '#',
'alive' => FALSE,
'thread' => '' );
/**
* Take an input and create that connection and connect to the database
* using the appropriate logins
* @param $type - Type of connection; SELECT, UPDATE, DELETE, INSERT
*/
public function __construct( $type ) {
switch($type) {
case "SELECT":
// Create the connection
$this->_SELECT['connection'] = new mysqli($this->_HOST,
$this->_SELECT['user'],
$this->_SELECT['pass'],
$this->_DATABASE );
// State that the connection is alive
$this->_SELECT['alive'] = TRUE;
// Put in the thread ID that is created when the connection is established
$this->_SELECT['thread'] = $this->_SELECT['connection']->thread_id;
// Verify that the connection was successfull
if($this->_SELECT['connection']->connect_error) {
die('Connection error: ' . $this->_SELECT['connection']->connect_errorno . ' ' .
$this->_SELECT['connection']->connect_error );
//TODO Create better error handling
} else {
echo "connection worked somehow.<br />";
}
case "INSERT":
// Create the connection
$this->_INSERT['connection'] = new mysqli($this->_HOST,
$this->_INSERT['user'],
$this->_INSERT['pass'],
$this->_DATABASE );
// State that the connection is alive
$this->_INSERT['alive'] = TRUE;
// Put in the thread ID that is created when the connection is establishedq
$this->_INSERT['thread'] = $this->_INSERT['connection']->thread_id;
// Verify that the connection was successfull
if($this->_INSERT['connection']->connect_error) {
die('Connection error: ' . $this->_INSERT['connection']->connect_errorno . ' ' .
$this->_INSERT['connection']->connect_error );
//TODO Create better error handling
} else {
echo "connection worked somehow.<br />";
}
case "DELETE":
// Create the connection
$this->_DELETE['connection'] = new mysqli($this->_HOST,
$this->_DELETE['user'],
$this->_DELETE['pass'],
$this->_DATABASE );
// State that the connection is alive
$this->_DELETE['alive'] = TRUE;
// Put in the thread ID that is created when the connection is establishedq
$this->_DELETE['thread'] = $this->_DELETE['connection']->thread_id;
// Verify that the connection was successfull
if($this->_DELETE['connection']->connect_error) {
die('Connection error: ' . $this->_DELETE['connection']->connect_errorno . ' ' .
$this->_DELETE['connection']->connect_error );
//TODO Create better error handling
} else {
echo "connection worked somehow.<br />";
}
case "UPDATE":
// Create the connection
$this->_UPDATE['connection'] = new mysqli($this->_HOST,
$this->_UPDATE['user'],
$this->_UPDATE['pass'],
$this->_DATABASE );
// State that the connection is alive
$this->_UPDATE['alive'] = TRUE;
// Put in the thread ID that is created when the connection is establishedq
$this->_UPDATE['thread'] = $this->_UPDATE['connection']->thread_id;
// Verify that the connection was successfull
if($this->_UPDATE['connection']->connect_error) {
die('Connection error: ' . $this->_UPDATE['connection']->connect_errorno . ' ' .
$this->_UPDATE['connection']->connect_error );
//TODO Create better error handling
} else {
echo "connection worked somehow.<br />";
}
}// END CASE
}// END _construct
public function get_Select_Con() {
return $this->_SELECT['connection'];
}
public function get_Insert_Con() {
return $this->_INSERT['connection'];
}
public function get_Delete_Con() {
return $this->_DELETE['connection'];
}
public function get_Update_Con() {
return $this->_UPDATE['connection'];
}
/**
* Kill the threads and close the connection
*/
public function __destruct() {
if ($this->_SELECT['alive'] == TRUE) {
$this->_SELECT['connection']->kill($this->_SELECT['thread']);
$this->_SELECT['connection']->close();
echo " thread killed and connection closed";
}
if ($this->_INSERT['alive'] == TRUE) {
$this->_INSERT['connection']->kill($this->_INSERT['thread']);
$this->_INSERT['connection']->close();
echo " thread killed and connection closed";
}
if ($this->_DELETE['alive'] == TRUE) {
$this->_DELETE['connection']->kill($this->_DELETE['thread']);
$this->_DELETE['connection']->close();
echo " thread killed and connection closed";
}
if ($this->_UPDATE['alive'] == TRUE) {
$this->_UPDATE['connection']->kill($this->_UPDATE['thread']);
$this->_UPDATE['connection']->close();
echo " thread killed and connection closed";
}
}// END _destruct
}
?>