Table of Contents
FleaPHP框架数据库查询条件($conditions)写法总结,fleaphpconditions
您可能感兴趣的文章:
Home php教程 php手册 FleaPHP框架数据库查询条件($conditions)写法总结,fleaphpconditions

FleaPHP框架数据库查询条件($conditions)写法总结,fleaphpconditions

Jun 13, 2016 am 08:43 AM
Database query

FleaPHP框架数据库查询条件($conditions)写法总结,fleaphpconditions

本文实例讲述了FleaPHP框架数据库查询条件($conditions)写法。分享给大家供大家参考,具体如下:

在FleaPHP中,凡是用到数据库查询的函数,都需要查询条件参数$conditions,现讲述用法如下:

举例:

// $conditions 保存查询条件
$conditions = 'level_ix > 1';
// $tableOrders 是一个订单数据表的表数据入口对象
$order = $tableOrders->find($conditions, 'created DESC', 'id, title, body');
$conditions = array('username' => 'dualface');
// $tableUsers 是一个用户信息数据表的表数据入口对象
$user = $tableUsers->find($conditions);
Copy after login

$conditions 参数可以是整数、字符串和数组三种类型:

1.如果 $conditions 参数是一个整数,则假定该整数为主键字段值。

// 查询主键字段值为1的记录
$user = $tableUsers->find(1);
// 如果主键字段名为"id",则生成的where字句为"WHERE `id` = 1"

Copy after login

2.如果 $conditions 参数是一个字符串,则该字符串将直接作为查询条件,这种方式可以支持最灵活的查询条件。 例如:

$conditions = 'id < 3'
$user = $tableUsers->find($conditions);
//生成的where字句为"WHERE id < 3"

Copy after login

3.1.如果 $conditions 参数是一个数组,且指定了键名和值,则查询条件中字段名为键名,字段值等于键值。例如:

// 查询id字段值为3的记录
$conditions = array(
  'id' => '1',
 );
$user = $tableUsers->find($conditions);
//生成的where字句为"WHERE `id` = 1"

Copy after login

3.2.如果 $conditions 参数是一个数组,但其中的元素没有键名, 则假定键值为自定义查询条件,例如:

$conditions = array('id = 1');
// 生成的where字句为"WHERE `id` = 1"
$user = $tableUsers->find($conditions);

Copy after login

3.3.$conditions 为数组时,可以混用字符串和键值对两种风格:

$conditions = array(
  'id < 3',
  'sex' => 'male',
);
$user = $tableUsers->find($conditions);
// 生成的where字句为"id < 3 AND `sex` = 'male'"

Copy after login

$conditions 为数组时,多个查询条件之间将使用 AND 布尔运算符进行连接。

3.4."in()"查询在FleaPHP中的实现。(原文由DreamPig发表于http://www.fleaphp.org/bbs/viewthread.php?tid=2168)
我们有时候要用到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)"

Copy after login

那么如果不是主键的话怎么写了呢? 也很简单,提供键值对即可。例如:

$condition = array(
  'in()' => array(
          'username' => array('username1','username2')
         )
  )
$user = $tableUsers->find($conditions);
// 生成的where子句为"WHERE `username` IN ('username1', 'username2')"

Copy after login

4.find()函数中其它参数的含义和用法如下:

4.1.$sort 参数指定查询时的排序方式,类型只能为字符串
例如 'created ASC' 表示按照"created"字段进行从小到大的排序。

4.2.$fields 参数指定查询结果中要包含哪些字段,类型可以为字符串或数组
当数据表的字段很多时,通过指定 $fields 参数可以避免查询不需要的字段,从而提高性能。

$fields 参数即可是以","逗号分隔的字段名,也可以是包含多个字段名的数组,例如:

$fields = array('title', 'created');
//也可以写成下面的字符串形式,两种写法作用相同,区别在于自动生成的字段名两边将会添加上"`"符号,以防止出现字段名与SQL关键字冲突的情况出现。建议手写时也加上"`"字符
$fields = 'title, created';
$user = $tableUsers->find('id < 10',NULL,$fields);

Copy after login

推荐使用数组,这样表数据入口处理起来更快一些。

希望本文所述对大家基于FleaPHP框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • php基于Fleaphp框架实现cvs数据导入MySQL的方法
  • fleaphp rolesNameField bug解决方法
  • fleaphp crud操作之find函数的使用方法
  • fleaphp crud操作之findByField函数的使用方法
  • fleaphp常用方法分页之Pager使用方法
  • FleaPHP的安全设置方法
  • fleaphp下不确定的多条件查询的巧妙解决方法
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem of database query number overflow in Java development How to solve the problem of database query number overflow in Java development Jun 29, 2023 pm 06:46 PM

How to solve the problem of database query number overflow in Java development. Title: How to solve the database query number overflow problem in Java development. Abstract: With the development of the Internet and the gradual increase in the amount of data, the number of database queries is also increasing. In Java development, due to memory limitations, you may encounter the problem of overflow in the number of database queries. This article will introduce several ways to solve this problem. Text: Optimizing database query statements First, we can solve this problem from the perspective of optimizing database query statements. we can use

How to query a database and display the results using PHP How to query a database and display the results using PHP May 02, 2024 pm 02:15 PM

Steps to use PHP to query the database and display the results: connect to the database; query the database; display the results, traverse the rows of the query results and output specific column data.

Laravel middleware: Add database querying and performance monitoring to your application Laravel middleware: Add database querying and performance monitoring to your application Jul 28, 2023 pm 02:53 PM

Laravel Middleware: Adding Database Query and Performance Monitoring to Applications Introduction: Data query and performance monitoring are very important when developing web applications. Laravel provides a convenient way to handle these requirements, namely middleware. Middleware is a technology that handles between requests and responses. It can perform some logic before the request reaches the controller or after the response is returned to the user. This article will introduce how to use Laravel middleware to implement database query and performance monitoring. 1. Create the middle

PHP database query tips: How to use the mysqli_query function to perform SQL queries PHP database query tips: How to use the mysqli_query function to perform SQL queries Jul 29, 2023 pm 04:42 PM

PHP database query tips: How to use the mysqli_query function to perform SQL queries When developing PHP applications, interacting with the database is a very important part. For query operations, PHP provides some built-in functions to execute SQL statements. This article will focus on how to use the mysqli_query function to help developers better perform database query operations. 1. Introduction to mysqli_query function The mysqli_query function is a built-in function of PHP.

Implement error handling mechanism for database queries in React Query Implement error handling mechanism for database queries in React Query Sep 28, 2023 pm 02:40 PM

Implementing the error handling mechanism of database queries in ReactQuery ReactQuery is a library for managing and caching data, and it is becoming increasingly popular in the front-end field. In applications, we often need to interact with databases, and database queries may cause various errors. Therefore, implementing an effective error handling mechanism is crucial to ensure application stability and user experience. The first step is to install ReactQuery. Add it to the project using the following command: n

Laravel development: How to use Eloquent ORM for database query? Laravel development: How to use Eloquent ORM for database query? Jun 14, 2023 pm 12:47 PM

Laravel is a popular PHP development framework that provides a series of tools and helper functions to speed up the development of web applications. Among them, EloquentORM is one of the tools used for database operations in the Laravel framework, allowing Laravel developers to query and operate the database more quickly. In this article, we will delve into how to use EloquentORM for database queries. Install EloquentORM First, we need to install

Front-end performance strategies for optimizing database queries in React Query Front-end performance strategies for optimizing database queries in React Query Sep 26, 2023 am 11:38 AM

Front-end performance strategies for optimizing database queries in ReactQuery In modern front-end development, we often need to interact with the back-end database to obtain data to render the page. However, frequent database queries can cause performance issues, especially when the page needs to render large amounts of data. In this case, we can use ReactQuery to optimize the front-end performance of database queries. ReactQuery is a JavaScr for managing data queries and state

PHP High Performance: How to Optimize Database Queries PHP High Performance: How to Optimize Database Queries Jun 04, 2023 am 08:40 AM

In the current Internet era, with the explosive growth of data, databases have become the core of a service. The performance and speed of the database directly affect the user experience and usability of the website and its applications. Therefore, how to optimize database queries is an issue that developers need to focus on. In the PHP language, through the optimization of database query statements, the performance of the program can be improved, the burden on the server can be reduced, and the stability of the service can be improved. This article will introduce how to optimize database queries from the following aspects: 1. Using indexes when performing queries

See all articles