-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostgres.php_example.php
More file actions
73 lines (65 loc) · 1.58 KB
/
Copy pathpostgres.php_example.php
File metadata and controls
73 lines (65 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
class postgres
{
public $db;
private $dbname = 'tu-base';
private $host = 'tu-servidor';
private $username = 'tu-usuario';
private $password = 'tu-passwd';
private $port = '5432'; //default
public function __construct()
{
$dbh = new PDO("pgsql:dbname=$this->dbname;host=$this->host;port=$this->port", $this->username, $this->password);
if ($dbh)
$this->db = $dbh;
else return 0;
}
public function insert ($table, $data)
{
$fields = '';
$values = '';
$sql = 'INSERT INTO '.$table.' (';
foreach ($data as $field => $value)
{
$fields.= $field.', ';
$values.= "'$value', ";
}
$sql.= substr($fields, 0, -2).') VALUES ('.substr($values, 0, -2).')';
$query = $this->db->query($sql);
}
public function update ($table, $data, $cond)
{
$values = '';
$sql = 'UPDATE '.$table.' SET ';
foreach ($data as $field => $value)
{
$values.= $field."='$value', ";
}
$sql.= substr($values, 0, -2).' WHERE '.$cond;
$query = $this->db->query($sql);
if ($query)
return 1;
else
return 0;
}
public function select ($table, $fields = '*', $cond = NULL, $order = NULL)
{
$sql = empty($cond) ? 'SELECT '.$fields.' FROM '.$table : 'SELECT '.$fields.' FROM '.$table.' WHERE '.$cond;
if (!empty($order))
$sql.= ' ORDER BY '.$order;
$query = $this->db->query($sql);
if ($query)
return $query->fetchAll(PDO::FETCH_OBJ);
else return 0;
}
public function eliminar($table, $cond)
{
$sql = 'DELETE From '.$table;
$sql.= ' WHERE '.$cond;
$query = $this->db->query($sql);
if ($query)
return 1;
else
return 0;
}
}