search
Homephp教程php手册ThinkPHP中order()使用方法详解,thinkphporder

ThinkPHP中order()使用方法详解,thinkphporder

本文介绍ThinkPHP的order()方法的用法。order方法可以用于对数据库操作的结果进行排序。即相当于是在select语句中一个order by的子句。

order方法属于模型的连贯操作方法之一,用于对数据库操作的结果进行排序。即相当于是在select语句中一个order by的子句。

用法

$Model->where('status=1')->order('id desc')->limit(5)->select();

注意:连贯操作方法没有顺序,可以在select方法调用之前随便改变调用顺序。
支持对多个字段的排序,例如:

$Model->where('status=1')->order('id desc,status')->limit(5)->select();

如果没有指定desc或者asc排序规则的话,默认为asc。

如果你的字段和mysql关键字有冲突,那么建议采用数组方式调用,例如:

$Model->where('status=1')->order(array('order','id'=>'desc'))->limit(5)->select();

补充:

Thinkphp无法使用->order() 排序的两种解决办法!

使用ThinkPHP,却发现无法使用->order($order)来排序。

$order = " info.date2 desc ";

很遗憾的是这样写结果order却变成了 order by date2 limit ... desc不见了。

解决办法一:

$order 里面两边不能有任何空格,$order = "info.date2 desc";(正确)。$order = " info.date2 desc";(错误!)

解决办法二:

打开文件:D:\WebSite\Zbphp.com\www\ThinkPHP\Extend\Model\ViewModel.class.php

修改第136行,改成 $array = explode(' ', trim($order));加上trim 保存即可,如图所示:

推荐使用第二种方法,不过期待thinkphp官网可以修正这个小问题,免得用户自己修改内核代码

关于ThinkPHP中order()使用方法就给大家介绍这么多,希望对您有所帮助,同时也非常感谢大家对帮客之家网站的支持!

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

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

Video Face Swap

Video Face Swap

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

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.