Home >Database >Mysql Tutorial >How should the database query method be implemented in PHP?

How should the database query method be implemented in PHP?

慕斯
慕斯Original
2021-06-22 15:12:272808browse

The previous article introduced to you "How to use PHP for database operations? 》, this article continues to introduce to you how to implement the database query method in PHP? ? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How should the database query method be implemented in PHP?

How to implement the query method: Based on the principles mentioned before, we need to use the following method:

//filed method

//table method

/ /where method

//group method

/ /having method

// order method

//limit method

, if we call one of them, then I will save one of them into an array, and finally we pass the select method Query, and finally return the results to us. Next we can present it in the form of code. The code is as follows:

We first define a (function) function; we use the field method. After passing it, we To determine whether the field has been passed, if it is not empty, we will continue to pass it down. If it is empty, we will directly return $this, which means that if it is not empty, then process it,

//field method:

function field($field)
{
//如果不为空,再进行处理
if (!empty($field)) {
if (is_ string($field)) {
$this->options['field'] = $field;
} else if (is_ array($field)) {
$this->options['field'] = join(',', $field);
}
}
return $this;
}

//table method:

Same first we also have to determine whether it is empty;

function table($table )
{
if(!empty ($table)) {
$this->options['table'] = $table;
}
return $this;
}
//where方法
function where ($where )
{
if (!empty($where)) {
$this->options[ 'where'] = 'where '.$where ;
}
return $this;
}

//group method

function group($group) 
if (!empty($group)) {
$this- >options[ ' group'] ='group by '.$group;
}
return $this;
}
//having方法
function having($having)
{
if (!empty ($having)) {
$this ->options['having'] = 'having'.$having;
}
return $this;
}

//order method

function order($order)
{
if (!empty($order)) {
$this->options['order'] = 'order by'.$order;
}
return $thiys;
}

//limit method

function limit($limit )
{
if (!empty($limit)) {
if (is_string($limit)) {
$this->options['limit'] ='limit'.$limit;
} else if (is_array($limit)) {
$this->options['limit'] = 'limit' . join(',',$limit);
}
}
}

The above are some of the methods we disclose to the public;

Recommended tutorial: "MySQL Tutorial"

The above is the detailed content of How should the database query method be implemented in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn