Home >Backend Development >PHP Tutorial >Summary of how to write FleaPHP framework database query conditions ($conditions), fleaphpconditions_PHP tutorial
This article describes how to write FleaPHP framework database query conditions ($conditions). Share it with everyone for your reference, the details are as follows:
In FleaPHP, any function that uses database query requires the query condition parameter $conditions. The usage is described as follows:
Example:
// $conditions 保存查询条件 $conditions = 'level_ix > 1'; // $tableOrders 是一个订单数据表的表数据入口对象 $order = $tableOrders->find($conditions, 'created DESC', 'id, title, body'); $conditions = array('username' => 'dualface'); // $tableUsers 是一个用户信息数据表的表数据入口对象 $user = $tableUsers->find($conditions);
$conditions parameter can be of three types: integer, string and array:
1. If the $conditions parameter is an integer, the integer is assumed to be the primary key field value.
// 查询主键字段值为1的记录 $user = $tableUsers->find(1); // 如果主键字段名为"id",则生成的where字句为"WHERE `id` = 1"
2. If the $conditions parameter is a string, the string will be directly used as the query condition. This method can support the most flexible query conditions. For example:
$conditions = 'id < 3' $user = $tableUsers->find($conditions); //生成的where字句为"WHERE id < 3"
3.1. If the $conditions parameter is an array, and the key name and value are specified, the field name in the query condition is the key name, and the field value is equal to the key value. For example:
// 查询id字段值为3的记录 $conditions = array( 'id' => '1', ); $user = $tableUsers->find($conditions); //生成的where字句为"WHERE `id` = 1"
3.2. If the $conditions parameter is an array, but the elements in it have no key names, it is assumed that the key value is a custom query condition, for example:
$conditions = array('id = 1'); // 生成的where字句为"WHERE `id` = 1" $user = $tableUsers->find($conditions);
3.3. When $conditions is an array, you can mix string and key-value pair styles:
$conditions = array( 'id < 3', 'sex' => 'male', ); $user = $tableUsers->find($conditions); // 生成的where字句为"id < 3 AND `sex` = 'male'"
When $conditions is an array, multiple query conditions will be connected using the AND Boolean operator.
3.4. Implementation of "in()" query in FleaPHP. (Original text published by DreamPig at http://www.fleaphp.org/bbs/viewthread.php?tid=2168)
We sometimes need to use operations like in, so how do we write it in condition?
// 假如主键名为"id",需要查询id的值为1、2、3其中之一,则可以这样写: $condition = array( 'in()' => array(1,2,3), ) $user = $tableUsers->find($conditions); // 生成的where子句为"WHERE `id` IN (1, 2, 3)"
So how to write it if it is not the primary key? It is also very simple, just provide the key-value pair. For example:
$condition = array( 'in()' => array( 'username' => array('username1','username2') ) ) $user = $tableUsers->find($conditions); // 生成的where子句为"WHERE `username` IN ('username1', 'username2')"
4.The meaning and usage of other parameters in the find() function are as follows:
4.1.$sort parameter specifies the sorting method during query, the type can only be string
For example, 'created ASC' means sorting from small to large according to the "created" field.
4.2. The $fields parameter specifies which fields to include in the query results. The type can be string or array
When the data table has many fields, specifying the $fields parameter can avoid querying unnecessary fields, thereby improving performance.
The $fields parameter can be field names separated by "," commas, or an array containing multiple field names, for example:
$fields = array('title', 'created'); //也可以写成下面的字符串形式,两种写法作用相同,区别在于自动生成的字段名两边将会添加上"`"符号,以防止出现字段名与SQL关键字冲突的情况出现。建议手写时也加上"`"字符 $fields = 'title, created'; $user = $tableUsers->find('id < 10',NULL,$fields);
It is recommended to use arrays, so that table data entry can be processed faster.
I hope this article will be helpful to everyone’s PHP programming based on the FleaPHP framework.