-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect.php
More file actions
88 lines (74 loc) · 2.02 KB
/
connect.php
File metadata and controls
88 lines (74 loc) · 2.02 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
class db_connect extends PDO
{
private $servername;
private $username;
private $password;
public function __construct($database)
/*
* Connect to database <database>.
*/
{
$this->servername = "localhost";
$this->username = "root";
$this->password = "root";
try {
parent::__construct("mysql:host=$this->servername;dbname=$database", $this->username, $this->password);
$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
// set PDO error mode to exception
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
}
public function query($query)
/*
* General query function, using prepared statements.
*
* Usage:
*
* // one line:
* $row=$result->fetch(PDO::FETCH_ASSOC);
* // multiple lines:
* while ($row=$result->fetch(PDO::FETCH_ASSOC))
* {
* //do things
* }
*/
{
$result = $this->prepare($query);
$result->execute();
return $result;
}
}
class db_io extends db_connect
{
protected $db_table;
protected $result;
public function __construct($database,$db_table)
{
parent::__construct($database);
$this->db_table=$db_table;
}
protected function write_on_db($row)
{
if (is_array($row))
{
foreach ($row as $s)
{
$string .= ",'".$s."'";
}
} else
{
$string=",'".$row."'";
}
$query="INSERT INTO `".$this->db_table."` VALUES (''".$string.")";
$this->query($query);
}
protected function read_from_db($what="*",$where="")
{
$where="" ? "" : " WHERE ".$where;
$query="SELECT ".$what." FROM `".$this->db_table."`".$where;
$this->result = $this->query($query);
}
}