我想在 PostgreSQL 数据库中显示多行到HTML表格,但我似乎无法做到正确。我设法显示单行(parsing as JSON the pg_fetch_assoc result)
但我不能对多行执行相同操作。我已经尝试在循环中使用pg_fetch_row
并将值存储到数组中但是我收到错误"JSON.parse: unexpected character".
有任何建议吗?
首先有一个DB连接文件。
然后我的模型就是这个
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);
答案 0 :(得分:1)
从数据库中检索数据后,使用utf8_encode()
将其转换为utf-8 echo json_encode(utf8_encode($model_customermeters));