我有这段代码:
function Perubahan($a = '`Ubah`') {
$con = mysqli_connect("localhost", "root", "", "nofriani"); //koneksi ke database
$syntax = 'SELECT' . $a . ' FROM `table 1` WHERE `No`= 6';
$naik = mysqli_query($con, $syntax);
while ($row = mysqli_fetch_array($naik)) {
echo round($row[0], 3);
}
}
function Jenis($b = 1) {
$con = mysqli_connect("localhost", "root", "", "nofriani"); //koneksi ke database
$syntax = 'SELECT `Jenis` FROM `table 1` WHERE `No`= ' . $b;
$naik = mysqli_query($con, $syntax);
while ($row = mysqli_fetch_array($naik)) {
echo $row[0];
}
}
function Andil($b = 1) {
$con = mysqli_connect("localhost", "root", "", "nofriani"); //koneksi ke database
$syntax = 'SELECT `Andil` FROM `table 1` WHERE `No`= ' . $b;
$naik = mysqli_query($con, $syntax);
while ($row = mysqli_fetch_array($naik)) {
echo round($row[0], 3);
}
}
function Kelompok($b = 1) {
$con = mysqli_connect("localhost", "root", "", "nofriani"); //koneksi ke database
$syntax = 'SELECT `Andil` FROM `table 1` WHERE `No`= ' . $b;
$naik = mysqli_query($con, $syntax);
while ($row = mysqli_fetch_array($naik)) {
echo round($row[0], 3);
}
}
所以我调用每个函数(仍然在同一个PHP文件中)。但是当我开始运行它时,显示结果花了太长时间(但它确实有效)。我猜这个问题是因为我在每个函数中重复数据库连接。 如何避免连接到同一个数据库?
我尝试创建与不同文件的数据库连接并在每个函数中调用该文件,但它没有工作。我还尝试传递$ con variable into the function(在每个函数中执行global $con
),但它也没有让它运行得更快。
我在这里错过了一些东西吗?
答案 0 :(得分:2)
您可以使用课程:
class NameService
{
private $con = null;
public function __construct()
{
$this->con = new mysqli("localhost", "root", "", "nofriani");
if ($this->con->connect_error) {
die('Connect Error (' . $this->con->connect_errno . ') '
. $this->con->connect_error);
}
}
public function __destruct()
{
$this->con->close();
}
function Andil($b = 1) {
$syntax = 'SELECT `Andil` FROM `table 1` WHERE `No`= ' . $b;
$result= $this->con->query($syntax);
while ($row = $result->fetch_array()) {
echo round($row[0], 3);
}
}
...
}
并使用它:
$nameService = new NameService();
$nameService->Andil(23);
编辑:现在它采用OOP风格
答案 1 :(得分:1)
如果可以使用Classes,那么可以为类创建全局变量,它保存与数据库的连接,而不是每个函数都使用相同的连接,并且不会重新连接。
使用预准备语句,因为现在您的代码很容易破解。
函数Jenis
,Andil
和Kelompok
与为核心重用准备的Perubahan
完全相同,因此只需将其重命名为getData($select = '*')
并仅使用一个功能。
答案 2 :(得分:0)
作为DRY的规则,您最好提取这部分代码:
mysqli_connect("localhost", "root", "", "nofriani"); //koneksi ke database
并放在一个地方(一个类或一个函数),所有其他代码都可以访问它。 例如,您可以使用以下函数:
function getConnection()
{
return mysqli_connect("localhost", "root", "", "nofriani"); //koneksi ke database
}
然后你可以这样做:
function Jenis($b = 1) {
$con = getConnection();
$syntax = 'SELECT `Jenis` FROM `table 1` WHERE `No`= ' . $b;
$naik = mysqli_query($con, $syntax);
while ($row = mysqli_fetch_array($naik)) {
echo $row[0];
}
}
这样一来,如果你的ConnectionString
被更改,你应该改变一个地方,所有其他功能都像以前一样工作。(顺便说一下,最好不要硬编码ConnectionString
)
答案 3 :(得分:0)
global $con;
// Requires a password here...
$con = new PDO('mysql:host=localhost;dbname=nofriani', 'root', $pass);
class QueryEngine
{
public function Fetch($_sql)
{
global $con;
// You could bind values here
$query = $con->prepare($_sql);
$query->execute();
if($query->rowCount() > 0) {
while($result = $query->fetch(PDO::FETCH_ASSOC)) {
$array[] = $result;
}
}
return (isset($array))? $array:0;
}
}
class NameEngine
{
protected $MySQL;
public function __construct()
{
// Create sql engine object
$this->MySQL = new QueryEngine();
}
public function execute($_name = false,$_value = 1) {
// If the name is not blank, run
if($_name !== false) {
// Fetch values to array (or do your round() function)
// I tend to just return arrays in classes and loop the array on an html page but you could do your echo here too.
$_result = $this->MySQL->Fetch("SELECT $_name FROM `table 1` WHERE `No`= '$value'");
}
// If name not set, just return empty/false
return (isset($_result))? $_result:0;
}
}
// Create name object
$_FetchNames = new NameEngine();
// Run name(s)
$_Jenis = $_FetchNames->execute('Jenis');
// Print result
print_r($_Jenis);
// Close connection
$con = NULL;