我一直在想下面的课程是否正确:
这个类当然应该在收集数据后建立一个mysql连接,它也会执行mysql的不同操作,例如'select','insert','update','delete'......等等
class MySQL {
protected $hostname;
protected $username;
protected $password;
protected $database;
protected $t_prefix;
protected $_connect;
protected $SQLquery;
public function mysql () {
$this->hostname = INNOPB_MYSQl_HOSTNAME;
$this->username = INNOPB_MYSQl_USERNAME;
$this->password = INNOPB_MYSQl_PASSWORD;
$this->database = INNOPB_MYSQl_DATABASE;
$this->t_prefix = INNOPB_MYSQL_T_PREFIX;
return $this;
}
public function connect( ) {
$this->_connect = mysql_connect(
$this->hostname,
$this->username,
$this->password
) or die ( mysql_error() );
mysql_select_db( $this->database , $this->_connect ) or die ( mysql_error() );
}
public function select ( $WHAT, $FROM, $WHERE = '', $ORDERBY = '', $ORDER = '', $LIMIT = '' ) {
$FROM = $this->t_prefix . $FROM;
$WHERE = ( !$WHERE ) ? '': " WHERE $WHERE";
$ORDERBY = ( !$ORDERBY ) ? '': " ORDER BY $ORDERBY";
$ORDER = ( !$ORDER ) ? '': " $ORDER";
$LIMIT = ( !$LIMIT ) ? '' : " LIMIT $LIMIT";
$FROM = str_replace( '"' , "'", $FROM );
$WHERE = str_replace( '"' , "'", $WHERE );
$ORDERBY = str_replace( '"' , "'", $ORDERBY );
$ORDER = str_replace( '"' , "'", $ORDER );
$LIMIT = str_replace( '"' , "'", $LIMIT );
$this->SQLquery = "SELECT $WHAT FROM $FROM$WHERE$ORDERBY$ORDER$LIMIT";
$this->SQLquery = mysql_query( $this->SQLquery );
return $this;
}
public function getQuery() { return $this->SQLquery; }
public function getRowsNum() {
return mysql_num_rows( $this->SQLquery );
}
public function delete( $FROM, $WHERE, $deleteAll = false ) {
if ( !$FROM || !$WHERE ) :
else :
$FROM = $this->t_prefix . $FROM;
$WHERE = ( !$WHERE ) ? '': " WHERE $WHERE";
$FROM = str_replace( '"' , "'", $FROM );
$WHERE = str_replace( '"' , "'", $WHERE );
( $deleteAll ) ? $this->SQLquery = "DELETE FROM $FROM" : $this->SQLquery = "DELETE FROM $FROM$WHERE";
$this->SQLquery = mysql_query( $this->SQLquery );
return $this->SQLquery;
endif;
}
}
答案 0 :(得分:2)
如果它有效,那么我认为没问题,而不是做一个特定的connect()
你应该使用function __construct()
,这样它就会自动连接到数据库,然后使用function __destruct()
来在课程结束后终止连接。