dongsi1944 2015-10-30 17:17
浏览 58
已采纳

通过PHP添加WHERE,SELECT,ORDER BY等...就像链式SQL一样

I need to add WHERE, SELECT, ORDER BY etc...like a chain SQL by means of PHP. I use https://github.com/greenlion/PHP-SQL-Parser to spilt up the SQL in Array and then to form the SQL

i need a intermediate code. i can to add SELECT,WHERE, ORDER BY, ETC.. and i can test if exist in the table, fields, in array of PHPSQLParser.

Example:

$sSql = 'Select a, b, c from table1 inner join table2 on (table1.a = table2.a) where b = 900';

# Return array SQL
$aParser = new PHPSQLParser( $sSql );
$aParser = $aParser->parsed;

// I need to intermediate code add fields, conditions, etc. //

// Create the SQL through the array
$creator = new PHPSQLCreator( $this->aParser );

// Show SQL
echo $creator->created;
  • 写回答

1条回答 默认 最新

  • dongzhouhao4316 2015-10-30 22:57
    关注

    The package you are using is missing methods you need to work with the parsed query. If you write your own "query builder" package, you can use this package as a starting point so you don't have to write the code that parses the sql. Or better yet, you can use a different package altogether that has both query parsing and building included.

    Here's one: https://github.com/jasny/dbquery-mysql

    It can be used in the way you are asking:

    <?php
        use Jasny\DB\MySQL\Query;
    
        $query = new Query("SELECT * FROM foo LEFT JOIN bar ON foo.bar_id = bar.id WHERE active = 1 LIMIT 25");
        if (isset($_GET['page'])) $query->page(3);
    
        $filter = isset($_POST['filter']) ? $_POST['filter'] : array(); // array('type' => 'bike', 'price between ? and ?' => array(10, 20))
        foreach ($filter as $field => $value) {
            $query->where($field, $value);
        }
    
        $result = $mysqli->query($query); // SELECT * FROM foo LEFT JOIN bar ON foo.bar_id = bar.id WHERE (active = 1) AND (`type` = "bike") AND (`price` between 10 and 20) LIMIT 25 OFFSET 50
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集