使用 PHP 显示 PostgreSQL 中的多行


Display multiple rows from PostgreSQL with PHP

我想将PostgreSQL数据库中的多行显示到HTML表,但我似乎无法正确处理。我设法显示单行(parsing as JSON the pg_fetch_assoc result)但我不能对多行做同样的事情。我尝试在循环中使用pg_fetch_row并将值存储到数组中,但出现错误"JSON.parse: unexpected character". 有什么建议吗?

首先有一个用于数据库连接的文件。

那么我的模型是这个

public function history_customermeters($meterSN){
        $db = new db_connect();
        $db -> connect();
        $query = 'SELECT * FROM hcd_customermeters WHERE "meterSN"=$1';
        $result = $db -> executeSQL($query, array($meterSN));
        $num_rows = pg_num_rows($result);
        $this -> $arrayAll = array();
        for($i=0; $i<$num_rows; $i++){
            $row = pg_fetch_assoc($result, $i);
            $this -> meterSN = $row['meterSN'];
            $this -> contractCode = $row['contractCode'];
            $this -> deviceManufacturer = $row['deviceManufacturer'];
            $this -> deviceType = $row['deviceType'];
            $this -> firstInstallationDate = $row['firstInstallationDate'];
            $this -> diameter = $row['diameter'];
            $this -> pipeID = $row['pipeID'];
            $this -> causeOfTest = $row['causeOfTest'];
            $this -> faultReportDate = $row['faultReportDate'];
            $this -> workshopIntroDate = $row['workshopIntroDate'];
            $this -> warehouseDeliveryDate = $row['warehouseDeliveryDate'];
            $this -> newInstallationDate = $row['newInstallationDate'];
            $this -> operatorCreator = $row['operatorCreator'];
            $this -> recordCreation = $row['recordCreation'];
            $this -> operatorUpdater = $row['operatorUpdater'];
            $this -> recordUpdate = $row['recordUpdate'];
            $this -> SRID = $row['SRID'];
            $this -> arrayAll[$i] = ($this -> meterSN, $this -> contractCode, $this -> deviceManufacturer,
                            $this -> deviceType, $this -> firstInstallationDate, $this -> diameter,
                            $this -> pipeID, $this -> causeOfTest, $this -> faultReportDate,
                            $this -> workshopIntroDate, $this -> warehouseDeliveryDate,
                            $this -> newInstallationDate, $this -> operatorCreator,
                            $this -> operatorCreator, $this -> recordCreation,
                            $this -> operatorUpdater, $this -> recordUpdate, $this -> SRID)
        }
        $db -> kill();
    }

我的控制器

  function getHistory($meterSN){
    $model_customermeters = new model_customermeters();
    $model_customermeters -> history_customermeters($meterSN);
    echo json_encode($model_customermeters);
}
    case 'get_history': $meterSN = $_REQUEST['meterSN']; getHistory($meterSN); break;

还有我的阿贾克斯电话

$.ajax({ url:'controller_customermeters.php', 
         data:{ 
         action:'get_history', 
         meterSN:$("#meterSN").val()}, 
         success:function(result){                                    
         var html = $.parseJSON(result); 

从数据库中检索数据后,使用 utf8_encode() 将其转换为 utf-8

    echo json_encode(utf8_encode($model_customermeters));