Advertisement
Guest User

Untitled

a guest
Jan 31st, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.98 KB | None | 0 0
  1. <?php
  2. class Database
  3. {
  4.     private $host;
  5.     private $port;
  6.     private $dbname;
  7.     private $user;
  8.     private $password;
  9.     protected $conn;
  10.  
  11.     public function __construct($host, $port, $dbname, $user, $password) {
  12.         $this->host = $host;
  13.         $this->port = $port;
  14.         $this->dbname = $dbname;
  15.         $this->user = $user;
  16.         $this->password = $password;
  17.     }
  18.  
  19.     function error($error, $id)
  20.     {
  21.         // throw new Exception($error, $id);
  22.         echo $error.", ". $id;
  23.         die;
  24.        
  25.     }
  26.  
  27.     public function connect()
  28.     {
  29.         $result = $this->conn = pg_connect("host=$this->host port=$this->port dbname=$this->dbname user=$this->user password=$this->password");
  30.         // $result = $this->conn = new PDO("pgsql:host=$this->host port=$this->port dbname=$this->dbname user=$this->user password=$this->password");
  31.  
  32.         if (!$result) {
  33.             return $this->error("Blad polaczenia", 1);
  34.         } else {
  35.             echo '<span style="float:right;color:red;background-color:black;">Połączono z bazą danych.</span><br />';
  36.         }
  37.     }
  38.  
  39.     public function query($query)
  40.     {
  41.         $ask = pg_query($query);
  42.         return $ask;
  43.     }
  44.  
  45. }
  46.  
  47. // $db = new Database("127.0.0.1", 5432, "blue", "postgres", "postgres");
  48. // $db->connect();
  49. // $db->query("SELECT * FROM test");
  50.  
  51. ?>
  52.  
  53.  
  54.  
  55. ________________________________________________________________________________________________________________________________
  56.  
  57. <?php
  58.  
  59. require "db.php";
  60.  
  61. class Result extends Database
  62. {
  63.     private $save;
  64.     public function __construct() {
  65.         parent::__construct("127.0.0.1", 5432, "blue", "postgres", "postgres");
  66.     }
  67.  
  68.     public function validate($value = null, $secondData = null)
  69.     {
  70.  
  71.         $sql = 'INSERT INTO test(surname) VALUES(:symbol)';
  72.         $stmt = $this->conn->prepare($sql);
  73.         $stmt->bindValue(':symbol', "cos_tam");
  74.         $stmt->execute();
  75.         return "save";
  76.  
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement