duanji8887 2017-02-16 15:26
浏览 51
已采纳

行错误:PDO中ORDER附近的语法错误

I was trying to get some data from an AccessDB file using MDBTools in PHP. All the normal SQL queries are working except ORDER BY and GROUP BY

When I use ORDER BY I am getting error

Error at Line : syntax error near ORDER
syntax error near ORDER
Got no result for 'SELECT * FROM CHECKINOUT ORDER BY CHECKTIME DESC' command

This is the code

$dataSourceName = "odbc:Driver=$driver;DBQ=$mdb_file;";
$conn = new PDO($dataSourceName);
$q = $conn->prepare("SELECT * FROM CHECKINOUT ORDER BY CHECKTIME DESC");
$q->execute();
$result = $q->fetchAll(PDO::FETCH_OBJ);
print_r($result);

[Update] When i used the above query in mysqli, it worked like charm. I created the database similar to the database on the access db file and used this code,

$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$row = $conn->query("SELECT USERID, CHECKTIME, CHECKTYPE FROM CHECKINOUT ORDER BY USERID DESC");
$result = $row->fetch_object();
print_r($result);

The above code worked like charm. But not when using PDO.

  • 写回答

1条回答 默认 最新

  • duandaijian1583 2017-02-16 16:28
    关注

    First does the query work without ORDER BY?

    On the local linux box i use connecting to an .MDB, I cannot use ORDER BY at all. Im not sure if this is due to the odbc drivers, or a configuration error somewhere on my end. Im pretty sure GROUP BY and ORDER BY just aren't supported. However i cant point you to documentation.

        $conn = new PDO("odbc:Driver=MDBTools; DBQ=$mdb_file; UID=; PWD=;");
        $q = $conn->prepare("SELECT col1, col2, col3 FROM some_table WHERE FINISHED = 0");
        $success = $q->execute();
        $error_info = $q->errorInfo(); // if needed
    
        $result = $q->fetchAll(PDO::FETCH_OBJ);
    
        aasort($rows, "col2");
    

    I would suggest getting the rows then sorting afterwards. It isn't what your asking but it accomplishes the same thing (or should)

    EDIT: sorry i forgot to include aasort, i thought the function was built in.

    function aasort (&$array, $key) {
        $sorter = [];
        $toreturn = [];
        reset($array);
    
        foreach ($array as $ii => $va)
            $sorter[$ii] = $va[$key];
    
        arsort($sorter);
    
        foreach ($sorter as $ii => $va)
            $toreturn[$ii] = $array[$ii];
    
        $array = $toreturn;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来