|  |  | 
          
            | ตัวอย่างครับ 
 pgsql.class.php
 
 class pgsql
{
    private $linkid;
    private $host;
    private $user;
    private $password;
    private $dbname;
    private $port;
    private $result;
    private $querycount;
    function __construct($host, $user, $password, $dbname, $port){
        $this->host = $host;
        $this->user = $user;
        $this->password = $password;
        $this->dbname = $dbname;
        $this->port = $port;
    }
    function connect(){
        try{
            $this->linkid = @pg_connect("host=$this->host dbname=$this->dbname user=$this->user password=$this->password port=$this->port");
            if (!$this->linkid){
                throw new Exception("{\"Status\":\"False\", \"ErrorMsg\":\"Could not connect to PostgreSQL server.\"}");
            }
        }
        catch (Exception $e){
            die($e->getMessage());
        }
    }
    function query($query){
        try{
            $this->result = @pg_query($this->linkid,$query);
            if(! $this->result){
                throw new Exception ("{\"Status\":\"False\", \"ErrorMsg\":\"The database query failed.\"}");
            }
        }
        catch (Exception $e){
            echo $e->getMessage();
        }
        $this->querycount++;
        return $this->result;
    }
    function affectedRows(){
        $count = @pg_num_rows($this->result);
        return $count;
    }
    function numRows(){
        $count = @pg_num_rows($this->result);
        return $count;
    }
    function fetchObject(){
        $row = @pg_fetch_object($this->result);
        return $row;
    }
    function fetchRow(){
        $row = @pg_fetch_row($this->result);
        return $row;
    }
    function fetchAssoc(){
        $row = @pg_fetch_assoc($this->result);
        return $row;
    }
    function fetchArray(){
        $row = @pg_fetch_array($this->result);
        return $row;
    }
}
 
 
 page.php
 
 define('DB_HOST','localhost');
define('DB_USER','postgres');
define('DB_PASS','xxx');
define('DB_NAME','xxx');
define('DB_PORT','5432');
function FetchJson($query){ //Fetch From PHP Script Query
        require ('pgsql.class.php');
        $pgsqldb = new pgsql (DB_HOST,DB_USER,DB_PASS,DB_NAME,DB_PORT);
        $pgsqldb->connect();
        $pgsqldb->query($query);
        $rows = array();
        while($row = $pgsqldb->fetchObject()){
            $rows[] = $row;
        }
        header('Content-type: application/json');
        return json_encode($rows, JSON_HEX_QUOT | JSON_HEX_TAG);
    }
        $query = "select * from table";
        print_r (FetchJson($query));
 
 
 Tag : PHP, PostgreSQL
 
 
 |  
            | 
 ประวัติการแก้ไข
 2015-06-23 18:01:45
 2015-06-23 18:02:33
 2015-06-23 18:03:04
 2015-06-23 18:04:21
 |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2015-06-23 17:56:59 | By :
                          jpong7 | View :
                          840 | Reply :
                          3 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |