连接成功运行时,mysqli和pdo返回0

Im trying to access the following table from my mysql localhost server hosted in mamp. I've tried multiple approaches, but all of them seem to provide the same result ("0 results"). What am I doing wrong? I provided both the PDO and the mysqli method, if someone could pinpoint the error, I would be very thankful!

CREATE TABLE `Orders` (
    OrderID INT(11),
    ParentOrderID INT(11),
    AccountID INT(11),
    RestaurantID INT(11),
    EmployeeID INT(11),
    -- CouponID INT(11),
    BillingID INT(11),
    ShippingID INT(11),
    CreateDate TIMESTAMP,
    OrderTime TIMESTAMP,
    IsTimeOrder BIT,
    IsDelivery BIT,
    PRIMARY KEY (OrderID)
)   ENGINE = INNODB;

the mysqli script i tried to use

<?php

$servername = "localhost";
$port = "3306";
$schema = "es_test";
$username = "root";
$password = "root";

$conn = new mysqli($servername, $username, $password, $schema, $port);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {
    echo "Connected successyfully <br>";
}

$sql = "
    SELECT
        *
    FROM
        `es_test`.`Orders`
    SORT BY
        `OrderID` ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "OrderID: " . $row["OrderID"] . " - CreateDate: " . $row["CreateDate"] . "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

and the PDO code

<?php
class Order{
    private $conn;
    private $table_name = "orders";
    //tableheaders

    public function __construct($db){
        $this->conn = $db;
    }

    function read(){
        $query = "
            SELECT 
                *
            FROM
                " . $this->table_name;

        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt;
    }
}

Why does it always keep returning 0 results? Is it my MySQL server that keeps rejecting it?

You use

SORT BY

In SQL, the syntax should be:

ORDER BY

See https://dev.mysql.com/doc/refman/8.0/en/select.html

You should also check for error cases after every call to query() or prepare() or execute(). Or else enable the driver to throw exceptions when you get errors.

In Mysqli, you do this by enabling the report mode before connecting:

mysqli_report(MYSQLI_REPORT_ALL);
$conn = new mysqli(...);

I tested your code and when I enabled exceptions I got this:

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right 
syntax to use near 'BY
        `OrderID` ASC' at line 5 in /Users/bkarwin/p.php:26

It returns the error on the word BY because it thinks you're using SORT as a table alias for the preceding table.

In PDO, you do this by enabling exceptions on error after connecting:

try {
    $dbh = new PDO($dsn, $user, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

If you don't enable automatic exceptions, then you will have to check the return value of query() and prepare() and execute(), which return FALSE if there's an error. That works the same in Mysqli and PDO.


Re your comment:

If you get the SQL right, the PDO code should work. But I would write it a bit differently:

function read(){

    $query = "
        SELECT 
            *
        FROM
            `{$this->table_name}`
        ORDER BY OrderID ASC";

    $stmt = $this->conn->prepare($query);
    $stmt->execute();
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

I assume you want to use ORDER BY the same as in your Mysqli code.

I avoid using string concatenation with . in PHP when building SQL queries. It's too easy for string concatenation to introduce mistakes.

I would also return the fetched rows, not the statement, from this function, to make it easier for the calling code.