


FleaPHP框架數(shù)據(jù)庫(kù)查詢(xún)條件($conditions)寫(xiě)法總結(jié),fleaphpconditions
Jun 13, 2016 am 08:43 AMFleaPHP框架數(shù)據(jù)庫(kù)查詢(xún)條件($conditions)寫(xiě)法總結(jié),fleaphpconditions
本文實(shí)例講述了FleaPHP框架數(shù)據(jù)庫(kù)查詢(xún)條件($conditions)寫(xiě)法。分享給大家供大家參考,具體如下:
在FleaPHP中,凡是用到數(shù)據(jù)庫(kù)查詢(xún)的函數(shù),都需要查詢(xún)條件參數(shù)$conditions,現(xiàn)講述用法如下:
舉例:
// $conditions 保存查詢(xún)條件 $conditions = 'level_ix > 1'; // $tableOrders 是一個(gè)訂單數(shù)據(jù)表的表數(shù)據(jù)入口對(duì)象 $order = $tableOrders->find($conditions, 'created DESC', 'id, title, body'); $conditions = array('username' => 'dualface'); // $tableUsers 是一個(gè)用戶(hù)信息數(shù)據(jù)表的表數(shù)據(jù)入口對(duì)象 $user = $tableUsers->find($conditions);
$conditions 參數(shù)可以是整數(shù)、字符串和數(shù)組三種類(lèi)型:
1.如果 $conditions 參數(shù)是一個(gè)整數(shù),則假定該整數(shù)為主鍵字段值。
// 查詢(xún)主鍵字段值為1的記錄 $user = $tableUsers->find(1); // 如果主鍵字段名為"id",則生成的where字句為"WHERE `id` = 1"
2.如果 $conditions 參數(shù)是一個(gè)字符串,則該字符串將直接作為查詢(xún)條件,這種方式可以支持最靈活的查詢(xún)條件。 例如:
$conditions = 'id < 3' $user = $tableUsers->find($conditions); //生成的where字句為"WHERE id < 3"
3.1.如果 $conditions 參數(shù)是一個(gè)數(shù)組,且指定了鍵名和值,則查詢(xún)條件中字段名為鍵名,字段值等于鍵值。例如:
// 查詢(xún)id字段值為3的記錄 $conditions = array( 'id' => '1', ); $user = $tableUsers->find($conditions); //生成的where字句為"WHERE `id` = 1"
3.2.如果 $conditions 參數(shù)是一個(gè)數(shù)組,但其中的元素沒(méi)有鍵名, 則假定鍵值為自定義查詢(xún)條件,例如:
$conditions = array('id = 1'); // 生成的where字句為"WHERE `id` = 1" $user = $tableUsers->find($conditions);
3.3.$conditions 為數(shù)組時(shí),可以混用字符串和鍵值對(duì)兩種風(fēng)格:
$conditions = array( 'id < 3', 'sex' => 'male', ); $user = $tableUsers->find($conditions); // 生成的where字句為"id < 3 AND `sex` = 'male'"
$conditions 為數(shù)組時(shí),多個(gè)查詢(xún)條件之間將使用 AND 布爾運(yùn)算符進(jìn)行連接。
3.4."in()"查詢(xún)?cè)贔leaPHP中的實(shí)現(xiàn)。(原文由DreamPig發(fā)表于http://www.fleaphp.org/bbs/viewthread.php?tid=2168)
我們有時(shí)候要用到in這樣的操作,那么在condition里面怎么寫(xiě)呢?
// 假如主鍵名為"id",需要查詢(xún)id的值為1、2、3其中之一,則可以這樣寫(xiě): $condition = array( 'in()' => array(1,2,3), ) $user = $tableUsers->find($conditions); // 生成的where子句為"WHERE `id` IN (1, 2, 3)"
那么如果不是主鍵的話(huà)怎么寫(xiě)了呢? 也很簡(jiǎn)單,提供鍵值對(duì)即可。例如:
$condition = array( 'in()' => array( 'username' => array('username1','username2') ) ) $user = $tableUsers->find($conditions); // 生成的where子句為"WHERE `username` IN ('username1', 'username2')"
4.find()函數(shù)中其它參數(shù)的含義和用法如下:
4.1.$sort 參數(shù)指定查詢(xún)時(shí)的排序方式,類(lèi)型只能為字符串
例如 'created ASC' 表示按照"created"字段進(jìn)行從小到大的排序。
4.2.$fields 參數(shù)指定查詢(xún)結(jié)果中要包含哪些字段,類(lèi)型可以為字符串或數(shù)組
當(dāng)數(shù)據(jù)表的字段很多時(shí),通過(guò)指定 $fields 參數(shù)可以避免查詢(xún)不需要的字段,從而提高性能。
$fields 參數(shù)即可是以","逗號(hào)分隔的字段名,也可以是包含多個(gè)字段名的數(shù)組,例如:
$fields = array('title', 'created'); //也可以寫(xiě)成下面的字符串形式,兩種寫(xiě)法作用相同,區(qū)別在于自動(dòng)生成的字段名兩邊將會(huì)添加上"`"符號(hào),以防止出現(xiàn)字段名與SQL關(guān)鍵字沖突的情況出現(xiàn)。建議手寫(xiě)時(shí)也加上"`"字符 $fields = 'title, created'; $user = $tableUsers->find('id < 10',NULL,$fields);
推薦使用數(shù)組,這樣表數(shù)據(jù)入口處理起來(lái)更快一些。
希望本文所述對(duì)大家基于FleaPHP框架的PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- php基于Fleaphp框架實(shí)現(xiàn)cvs數(shù)據(jù)導(dǎo)入MySQL的方法
- fleaphp rolesNameField bug解決方法
- fleaphp crud操作之find函數(shù)的使用方法
- fleaphp crud操作之findByField函數(shù)的使用方法
- fleaphp常用方法分頁(yè)之Pager使用方法
- FleaPHP的安全設(shè)置方法
- fleaphp下不確定的多條件查詢(xún)的巧妙解決方法

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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

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.

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

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

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.

With the continuous development of Internet technology, the explosive growth of data volume and the widespread application of various text data, full-text retrieval has become a very important technology. Full-text search is a method that can quickly and accurately find text data. It is widely used in application scenarios such as search engines, forums, blogs, and e-commerce websites. How to implement full-text search in PHP programming? 1. What is full-text search? In traditional relational databases, we usually use SQL statements for fuzzy queries. However, when the amount of data is large, this query method will

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

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
