0

我想将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 调用

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

1 回答 1

1

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

    echo json_encode(utf8_encode($model_customermeters));
于 2013-10-31T11:00:34.843 回答