使用PHP将输入的SQL查询格式化为HTML? [英] Use PHP to format an input SQL query as HTML?
问题描述
我正在寻找的是一个php函数,它需要一个像这样的无格式查询:
What I am looking for is a php function that takes an unformatted query like this:
$ sql =从价格= asc的颜色='绿色'顺序中选择名称和大小";
$sql = "select name, size from things where color = 'green' order by price asc";
,这样它就会出现在HTML页面中,如下所示:
so that it would appear in an HTML page something like this:
SELECT
name, size
FROM
things
WHERE
color = 'green'
ORDER BY
price ASC';
phpMyAdmin内部已经有一些代码可以执行此操作,我想我可以在其中查看!
There's some code inside phpMyAdmin that does this already, I could look in there I guess!
推荐答案
我遇到了同样的问题,并制作了一个轻量级的PHP类来进行格式/语法突出显示.
I had the same problem and made a light-weight PHP class to do formatting/syntax highlighting.
https://github.com/jdorn/sql-formatter
我还没有使用复杂的查询(子选择,并集等)对它进行全面测试,但是对于常见情况,它似乎工作得很好.
I haven't fully tested it with complex queries (sub-selects, unions, etc.), but it seems to work pretty well for common cases.
要获得完全准确的结果,您确实需要像phpMyAdmin这样使用的完整SQL解析器,但是它使用10,000行以上的代码散布在许多文件中,对于简单的调试来说可能会显得过头.
To get fully accurate results, you really need a full SQL parser like phpMyAdmin uses, but that uses 10,000+ lines of code spread out over many files and is probably overkill for simple debugging.
这篇关于使用PHP将输入的SQL查询格式化为HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!