【发布时间】:2023-03-17 18:16:01
【问题描述】:
我对 PHP 和 MySQL 的 OOP 有疑问。
我正在使用 OOP 使用 PHP 7,我想创建一个类 Connection。在这些课程中
我有一种方法可以生成对数据库的查询并将其存储在数组中。我希望在另一个文件的另一个类中打开该数组的结果。
如果我使用 if 语句,则发送单个值,如果我使用 while 循环,则在我请求另一个文件中的向量时不会显示任何内容。
我想创建此方法以避免重写对与数据库的连接的调用。
这是将继承显示数据的类的代码。
public function open_connection()
{
$this->connect = new mysqli( self::$SERVER, self::$USER, self::$PASS, $this->DB ) or die ( "Error" );
}
public function close_connection()
{
mysqli_close( $this->connect );
}
protected function execute_a_fetch_query( )
{
$this->open_connection();
$orderA = $this->connect->query( $this->query );
$orderB = $this->connect->query( $this->query );
if ( $this->rows = mysqli_fetch_array( $orderA ) ) { //this sentence avoid a duplicate result from the query
if ( $this->rows = mysqli_fetch_array( $orderB ) );
}
$this->close_connection();
}
这里是 Show 类中的另一个方法
public function data( $attributes = array() )
{
$this->query = 'select * from Supplier';
$this->execute_a_fetch_query();
echo '<tr>';
for ( $i = 0; $i < count( $this->_attributes ); $i++ ) {
echo
'<td>'. $this->rows[ $attributes[ $i ] ]. '</td>';
}
【问题讨论】: