• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

PHP Statements

Itutorial

Legendary OT User
Joined
Dec 23, 2014
Messages
2,338
Solutions
68
Reaction score
1,022
Trying to change my sql code to statements but can't figure this out.

PHP:
function getServerList($startID, $endID) {
        $conn = connectDB();

        $stmt = $conn->prepare("SELECT * FROM servers WHERE id >= ? AND id <= ?");
        $stmt->bind_param('ii', $startID, $endID);
      
        $serverArrays = array();
      
        if ($result = $stmt->execute()) {
            do {
                while ($row = $result->fetch_assoc()) {
                    $tmpArray = array();
                    $tmpArray[0] = $row['id'];
                    $tmpArray[1] = $row['name'];
                    $tmpArray[2] = $row['ip'];
                    $tmpArray[3] = $row['protocol'];
                    $tmpArray[4] = $row['port'];
                    $tmpArray[5] = $row['players'];
                    $tmpArray[6] = $row['maxplayers'];
                    $tmpArray[7] = $row['exprate'];
                    $tmpArray[8] = $row['website'];      
                    array_push($serverArrays, $tmpArray);
                }
                $result->free_result();
            } while ($result->next_result());
        }
      
        $conn->close();
        return $serverArrays;
    }

It seems like getting error messages with statements isnt very easy.
 
Solution
Figured it out. I wasn't getting any exceptions because the code all worked in the sense it wasn't any errors. Just calling things wrong.

PHP:
function getServerList($startID, $endID) {
        $conn = connectDB();

        $sql = "SELECT * FROM servers WHERE id >= ? AND id <= ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param('ii', $startID, $endID);
        $stmt->execute();
        
        $serverArrays = array();
        
        if($result = $stmt->get_result()) {
            do {
                while ($row = $result->fetch_assoc()) {
                    $tmpArray = array();
                    $tmpArray[0] = $row['id'];
                    $tmpArray[1] = $row['name'];
                    $tmpArray[2] =...
Wrap it around a try catch and print the the error
PHP:
try{
    code...
} catch(Exception $e){
    print_r($e);
    exit; // stop the code from running
}
 
Last edited:
Figured it out. I wasn't getting any exceptions because the code all worked in the sense it wasn't any errors. Just calling things wrong.

PHP:
function getServerList($startID, $endID) {
        $conn = connectDB();

        $sql = "SELECT * FROM servers WHERE id >= ? AND id <= ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param('ii', $startID, $endID);
        $stmt->execute();
        
        $serverArrays = array();
        
        if($result = $stmt->get_result()) {
            do {
                while ($row = $result->fetch_assoc()) {
                    $tmpArray = array();
                    $tmpArray[0] = $row['id'];
                    $tmpArray[1] = $row['name'];
                    $tmpArray[2] = $row['ip'];
                    $tmpArray[3] = $row['protocol'];
                    $tmpArray[4] = $row['port'];
                    $tmpArray[5] = $row['players'];
                    $tmpArray[6] = $row['maxplayers'];
                    $tmpArray[7] = $row['exprate'];
                    $tmpArray[8] = $row['website'];
                    
                    array_push($serverArrays, $tmpArray);
                }
                $result->free_result();
            } while ($conn->next_result());
        }

        $conn->close();
        return $serverArrays;
    }
 
Solution
is your connectDB() using exception error reporting tho? MySQLi's default error reporting more is a horrible silent-error-mode where you're supposed to check $stmt->errno after every query. to get MySQLi to throw exceptions on errors (as it should), connectDB() must set the exception error mode manually, does it do so? here's how to set it to exception mode fwiw:
Code:
(new mysqli_driver())->report_mode = MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX;
 
Last edited:
Back
Top