0

I want to display multiple rows from a PostgreSQL database to an HTML table but I can't seem to get it right. I managed to display a single row (parsing as JSON the pg_fetch_assoc result) but I can't do the same with multiple rows. I've tried with pg_fetch_row in a loop and storing the values to an array but I get an error "JSON.parse: unexpected character". Any advice?

First there is a file for the DB connection.

Then my model is this

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();

    }

My controller

  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;

And my ajax call

$.ajax({ url:'controller_customermeters.php', 
         data:{ 
         action:'get_history', 
         meterSN:$("#meterSN").val()}, 
         success:function(result){                                    
         var html = $.parseJSON(result); 
1
  • If you post the code that you have so far here, more people will be able to give a more accurate solution. Commented Oct 31, 2013 at 10:42

1 Answer 1

1

after retrive data from database, convert it in utf-8 with utf8_encode()

    echo json_encode(utf8_encode($model_customermeters));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.