简体   繁体   中英

Error at Line : syntax error near ORDER in PDO

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM