PHP实现SQL语句格式化功能的方法

2025-05-29 0 62

本文实例讲述了php实现sql语句格式化功能的方法。分享给大家供大家参考,具体如下:

一、问题:

要求使用php实现针对sql语句的格式化功能

二、解决方法:

这里使用github上的开源代码实现sql格式化功能。

github下载地址:https://github.com/till/sql-formatter

使用方法:

include包含sqlformatter.php文件,针对sql语句使用format方法即可实现格式化功能。

具体代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12
<?php

include('sqlformatter.php');

$sql="select date_format(b.t_create, '%y-%c-%d') dateid, b.title memo

from (select id from orc_scheme_detail d where d.business=208

and d.type in (29,30,31,321,33,34,3542,361,327,38,39,40,41,42,431,4422,415,4546,47,48,'a',

29,30,31,321,33,34,3542,361,327,38,39,40,41,42,431,4422,415,4546,47,48,'a')

and d.title is not null and t_create >=

date_format((date_sub(now(),interval 1 day)),'%y-%c-%d') and t_create

< date_format(now(), '%y-%c-%d') order by d.id limit 2,10) a,

orc_scheme_detail b where a.id = b.id";

echo sqlformatter::format($sql);

?>

运行结果如下图所示:

PHP实现SQL语句格式化功能的方法

ps:更多的用法还可参考源码中的examples.php示例

希望本文所述对大家php程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP实现SQL语句格式化功能的方法 https://www.kuaiidc.com/94169.html

相关文章

发表评论
暂无评论