如果我在我的php应用程序中连接到数据库的对象,可以说我正在使用mysqli作为我的数据库事务的对象。使用mysqli对象连接到使用php对象的mysql的最佳方法
例如:
$dbase = new mysqli('localhost','dbuser','dbpass','dbname');
$oresult = $dbase->query("SELECT `field` FROM `table` WHERE `otherfield` = 12;");
if($oresult->num_rows > 0) {
$row = $oresult->fetch_row();
$data = $row[0];
}
,但我有我想跟以dBASE另一个自定义对象。
<?php
class Thing {
private $sql = '';
public $results = '';
public function __construct($sql) {
$this->sql = $sql;
$this->get_data();
}
private function get_data() {
// get the stuff from the dbase using $this->sql
$this->results = 'whatever';
}
}
$thing = new Thing("SELECT `field` FROM `table` WHERE 1");
// do whatever i want with $thing->results
?>
哪里有“//从使用$这个 - 与dBASE> SQL的东西”行,我会想连接到dBASE和获取数据。
是它最好创建一个新的mysqli对象(我看到,因为我需要得到传递到每个对象我有连接信息的问题),或者我可以以某种方式使用
引用对象我已经有了global $dbase
里面的get_data函数。
什么是最佳实践?