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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀