Php 如何通过类函数显示获取的数据库数据
我是OOP新手,需要一些帮助我有一些代码可以从数据库中选择和获取数据,但我不知道如何显示获取的数据Php 如何通过类函数显示获取的数据库数据,php,mysql,function,class,fetch,Php,Mysql,Function,Class,Fetch,我是OOP新手,需要一些帮助我有一些代码可以从数据库中选择和获取数据,但我不知道如何显示获取的数据 /** * Connect to mysql database * * @return bool; */ public function Connect() { $settings = database_settings(); $connect = new mysqli($settings->host, $settings->user, $settings-&g
/**
* Connect to mysql database
*
* @return bool;
*/
public function Connect() {
$settings = database_settings();
$connect = new mysqli($settings->host, $settings->user, $settings->password, $settings->database);
if (!$connect->connect_errno) {
return $connect;
} else {
return false;
}
}
/**
* Prepare a mysqli query
*
* @return bool;
*/
public function statement($query) {
if (!empty($query)) {
$this->query = $query;
return true;
}
return false;
}
/**
* Execute a mysqli query and store result in memory
*
* @return bool;
*/
public function execute() {
$this->database = $this->Connect();
if (isset($this->query) && !empty($this->query)) {
$this->database->set_charset("utf8");
$this->exe = $this->database->query($this->query);
if (!isset($this->exe)) {
throw new DatabaseException("{$this->database->error} \n {$this->query} ");
}
if (isset($this->database->insert_id)) {
$this->last_id = $this->database->insert_id;
}
unset($this->query);
$this->database->close();
return true;
}
return false;
}
/**
* Prepare a query to select data from database
*
* @params = array();
* $params['from'] = names of table
* $params['params'] = names of columns which you want to select
* $params['wheres'] = specify a selection criteria to get required records
*
* @return bool;
*/
public function select($params, $multi = '') {
if (is_array($params)) {
if (!isset($params['params'])) {
$parameters = '*';
} else {
$parameters = implode(', ', $params['params']);
}
$order_by = '';
if (!empty($params['order_by'])) {
$order_by = "ORDER by {$params['order_by']}";
}
$where = '';
if (isset($params['wheres']) && is_array($params['wheres'])) {
$where = implode(' ', $params['wheres']);
}
$wheres = '';
if (!empty($params['wheres'])) {
$wheres = "WHERE({$where})";
}
$limit = '';
if (!empty($params['limit'])){
$limit = "LIMIT {$params['limit']}";
}
$query = "SELECT {$parameters} FROM `{$params['from']}` {$wheres} {$order_by} {$limit};";
$this->statement($query);
if ($this->execute()) {
return $this->fetch($multi);
}
}
return false;
}
/**
* Fetch the data from memory that is stored during execution;
*
* @params = $data = (ture if you want to fetch all data , or flase if only one row)
*
* @return bool;
*/
public function fetch($data = false) {
if (isset($this->exe)) {
if ($data !== true) {
if ($fetch = $this->exe) {
return arrayObject($fetch->fetch_assoc());
}
}
if ($data === true) {
if ($fetch = $this->exe) {
while ($all = $fetch->fetch_assoc()) {
$alldata[] = arrayObject($all);
}
}
if (isset($alldata) && !empty($alldata)) {
return arrayObject($alldata);
}
}
}
return false;
}
然后我选择了数据,但无法显示:
$params['from'] = 'category';
$params['order_by']= 'id';
$params['wheres'] = 'active="1"';
$params['limit']= '1';
$connect->select($params);
与显示任何变量的方式相同?我不确定这里的问题是什么,但听起来它与这个数据库类没有任何关系。或者问题是您无法从内存中获取存储的数据?我获取数据,但不知道如何从功能中访问它们