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 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿