我有以下代码可行,但我想知道这种技术的更好方法是什么?我想开始重用连接,而不是为每个查询创建新实例。 我的代码的任何实用建议和解决方案?
<?php
class dbc {
function openDb() {
$dbserver = '';
$dbusername = '';
$dbpassword = '';
$dbname = '';
try {
$db = new PDO('mysql:host=' . $dbserver . ';port=3306;dbname=' . $dbname . ';charset=utf8', '' . $dbusername . '', '' . $dbpassword . '', array(PDO::MYSQL_ATTR_INIT_COMMAND =>"SET SESSION time_zone = 'America/Chicago'"));
$timezone = "America/Chicago";
$db->exec("SET time_zone = '{$timezone}'");
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die("error, please try again");
}
return $db;
}
}
<?php
require 'dbc.php';
function getDailyProfitability() {
$db = new dbc();
$query = "SELECT 1 FROM DUAL";
$stmt = $db->openDb()->prepare($query);
$stmt->execute();
return $stmt->fetchAll();
}
?>