-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsql-mysql.php
96 lines (69 loc) · 1.84 KB
/
sql-mysql.php
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
88
89
90
91
92
93
94
95
96
<?php
class DB_MySQL extends mysqli
{
private $prefix;
function __construct($host, $user, $pass, $db, $tprefix)
{
parent::__construct($host, $user, $pass, $db);
if ($this->connect_error)
die("Connection Error: ".$this->connect_error);
$this->prefix = $tprefix;
}
function create($table, $args, $auto)
{
$str = "";
foreach ($args as $f => $v)
$str .= "`$f` $v NOT NULL, ";
$str = rtrim($str, ", ");
$str = str_replace(array("INTEGER", "TEXT"), array("int(9)", "varchar(20)"), $str);
if ($auto)
return $this->query("CREATE TABLE `{$this->prefix}$table` (`ID` smallint(2) NOT NULL AUTO_INCREMENT, $str, PRIMARY KEY (`ID`)) AUTO_INCREMENT=1;");
else
return $this->query("CREATE TABLE `{$this->prefix}$table` ($str);");
}
function insert($table, $args)
{
$fields = "";
$values = "";
foreach ($args as $f => $v)
{
$fields .= "`$f`, ";
$values .= "'$v', ";
}
$fields = rtrim($fields, ", ");
$values = rtrim($values, ", ");
return $this->query("INSERT INTO `{$this->prefix}$table` ($fields) VALUES ($values);");
}
function fetch($table, $value)
{
$result = $this->query("SELECT * FROM `{$this->prefix}$table` WHERE `ID` = $value;");
return $result->fetch_array(MYSQLI_ASSOC);
}
function fetchall($table, $opts = "")
{
$result = $this->query("SELECT * FROM `{$this->prefix}$table` $opts;");
$row = array();
$i = 0;
while ($res = $result->fetch_array(MYSQLI_ASSOC))
{
if (!isset($res['ID'])) continue;
foreach ($res as $key => $value)
{
$row[$i][$key] = $value;
}
$i++;
}
return $row;
}
function update($table, $id, $args)
{
$str = "";
foreach ($args as $f => $v)
{
$str .= "`$f` = '$v', ";
}
$str = rtrim($str, ", ");
return $this->query("UPDATE `$table` SET $str WHERE `ID` = '$id';");
}
}
?>