简体   繁体   中英

PHP Cannot Fetch larger data from database

Function getUsers_Test() can retrieve data with the limit of 10 rows only while my getUsers_Orig() cannot retrieve any records **my record in the database is almost 50 rows*. No error, no warning. (PHP version 5.5.12)

function getUsers_Test(){
    $sql = "SELECT TOP 10 a.user_name, a.user_level, a.user_status, b.* FROM user_access a, user_details b WHERE a.emp_code = b.emp_code ORDER BY a.id DESC";
    try{
        $data = array();
        $db = null;
        $db = connectDB();
        $stmt = $db->prepare($sql);
        $stmt->execute();
        $data = $stmt->fetchAll();

    }catch(PDOException $e){
        $data['message'] = "Error: " . $e;
    }
    echo json_encode($data);
}

    function getUsers_Orig(){
    $sql = "SELECT a.user_name, a.user_level, a.user_status, b.* FROM user_access a, user_details b WHERE a.emp_code = b.emp_code ORDER BY a.id DESC";
    try{
        $data = array();
        $db = null;
        $db = connectDB();
        $stmt = $db->prepare($sql);
        $stmt->execute();
        $data = $stmt->fetchAll();

    }catch(PDOException $e){
        $data['message'] = "Error: " . $e;
    }
    echo json_encode($data);
}

    function connectDB() {
    $dbuser = "myusername";
    $dbpass = "mypassword";
    $dbh = new PDO('odbc:databaseName', $dbuser, $dbpass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $dbh;
}

You could try catching a general exception instead of the PDOException

try
{
  // ...
} catch (\Exception $e) {
  // ...
}

The size of data is not a problem here, the data I was fetching has a special character (A' for Ñ). I use to add charset UTF-8 in my connection but still, I wasn't able to fetch it.

This problem occurs when I perform Generate Script and extract data from MSSQL instead of Backup and Restore. So when I execute the script from the Generate Script feature of MSSQL, the character changed and I didn't notice it.

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