Php MySQL关闭时间

Php MySQL关闭时间,php,mysql,mysqli,Php,Mysql,Mysqli,我编写了一个类来加载数据并通过MySQLi以SMARTY格式导出 public function myRow($sql) { $this->connect(); $rec = $this->conn->query($sql); $this->recordCount = $rec->num_rows; if ($this->makeRecordCount) { $this->totalRecordCount

我编写了一个类来加载数据并通过MySQLi以SMARTY格式导出

public function myRow($sql)
{
    $this->connect();
    $rec = $this->conn->query($sql);
    $this->recordCount = $rec->num_rows;

    if ($this->makeRecordCount) {
        $this->totalRecordCount = $this->recordCount;
    }

    if ($this->recordCount > 0) {
        $names = array();
        $result = array();
        $temp = array();
        $count = $rec->field_count;

        // Get fields name
        while ($fields = mysqli_fetch_field($rec)) {
            $names[] = $fields->name;
        }

        while ($row = $rec->fetch_assoc()) {
            foreach ($names as $name) {
                $temp[$name] = $row[$name];
            }

            array_push($result, $temp);
        }
    } else {
        $result = null;
    }

    $this->conn->close();

    return $result;
}
然后我可以做一些类似的事情

$sql = "SELECT * FROM `table`";
$datas = $class->myRow($sql);
$smarty->assign('datas', $datas);
$class->connect();
$sql = "SELECT * FROM `table`";
$datas = $class->myRow($sql);
$smarty->assign('datas', $datas);

$sql = "SELECT * FROM `table2`";
$datas = $class->myRow($sql);
$smarty->assign('data2s', $data2s);
$class->close();
可能有很多数据需要在一个页面中加载,我只想连接到数据库一次,但我想在课堂上完成这一切,我不想做类似的事情

$sql = "SELECT * FROM `table`";
$datas = $class->myRow($sql);
$smarty->assign('datas', $datas);
$class->connect();
$sql = "SELECT * FROM `table`";
$datas = $class->myRow($sql);
$smarty->assign('datas', $datas);

$sql = "SELECT * FROM `table2`";
$datas = $class->myRow($sql);
$smarty->assign('data2s', $data2s);
$class->close();

我觉得这很难看,但如果我在课堂上这样做,那就意味着我在加载每个数据时打开和关闭连接,如何做得更漂亮?

也许我错了,但您不需要强制关闭mysql连接,因为如果连接不是持久的,php
垃圾收集器将在脚本执行后关闭所有连接


因此,我建议您不要强制关闭mysql,让垃圾收集器来处理此任务,只有在您确定不再需要mysql事务时,才可以自己关闭连接。

您的第二个建议是我会做的

$class->connect();
$sql = "SELECT * FROM `table`";
$datas = $class->myRow($sql);
$smarty->assign('datas', $datas);

$sql = "SELECT * FROM `table2`";
$datas = $class->myRow($sql);
$smarty->assign('data2s', $data2s);
$class->close();
连接到数据库一次。由于PHP是单线程的,您将加载第一个结果,然后立即加载第二个结果。完成所有操作后,关闭连接。没有一个连接能够比它必须保持的时间更长,这是很好的

我通常所做的是创建一个与Smarty关联的方法,该方法也会关闭我的数据库连接。这样我就不用担心关上它了

大概是这样的:

<?php
// Store reference to Smarty in Class
$class->setSmarty($smarty);

[...]

// Done with all database fetching, now display the template
$class->display('my_template.tpl');

[...]

// Example inplementation of the class
Class YourClass {
    private $smarty;
    public function setSmarty($smarty) {
        $this->smarty = &$smarty;
    }
    public function display($tpl) {
        $this->close();
        $this->smarty->display($tpl);
    }
}
?> 

您不需要(也不应该)在
myRow()函数中打开/关闭连接

选项1(简单方法):在类级别处理连接

class MyDAOClass {
    private static $connection = null;

    public function __construct() {
        if (self::$connection === null) {
             // establish connection here
        }
    }

    public function myRow(...) {
        // use self::$connection here
    }
}
备选案文2:

处理来自类外部的连接(可能在单例类中),因为应用程序中的所有对象可能共享同一个对象