Tips voor database classe
Bij deze mijn eerste classe.
Zouden jullie hier tips voor willen geven?
Onderaan staat een klein voorbeeld.
Code (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
97
98
99
100
101
102
103
104
105
106
107
108
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
97
98
99
100
101
102
103
104
105
106
107
108
<?php
class database{
private $db_server = "localhost";
private $db_user = "username";
private $db_pass = "*******";
private $db_database = "database";
public $connection;
public function connect(){
$this->connection = mysqli_connect($this->db_server, $this->db_user, $this->db_pass, $this->db_database);
}
public function userInfo($username, $columns=array()){
$items = '';
foreach($columns as $column){
$items .= $column.', ';
}
$items = substr($items, 0, -2);
$usersql = "SELECT $items FROM users WHERE username='".mysqli_real_escape_string($this->connection, $username)."'";
$usersel = mysqli_query($this->connection, $usersql);
return mysqli_fetch_assoc($usersel);
}
public function tryLogin($username, $wachtwoord){
$usersql = "SELECT id, username, password FROM users WHERE username='".mysqli_real_escape_string($this->connection, $username)."' AND password='".md5($wachtwoord)."'";
$usersel = mysqli_query($this->connection, $usersql);
$info = mysqli_fetch_assoc($usersel);
if($info['id'] == null){
return 1; // verkeerde inloggegevens
}else{
return 2; // ingelogd
}
}
private function sqlError($sql, $error){
echo '<span style="color: #FF0000; font-weight: bold;">Er is iets fout gegaan. Onze excuses hiervoor.<br>De foutmelding is automatisch verzonden en zal zo snel mogelijk worden opgelost.</span>';
mail("[email protected]", "sql error", 'SQL = '.$sql.'
mysqli_error = '.$error);
return true;
}
public function query($sql, $type=null){
if($type==null){
$query = @mysqli_query($this->connection, $sql);
if($query){
return $query;
}else{
return $this->sqlError($sql, mysqli_error($this->connection));
}
}elseif($type == "object"){
$query = @mysqli_query($this->connection, $sql);
if($query){
return mysqli_fetch_object($query);
}else{
return $this->sqlError($sql, mysqli_error($this->connection));
}
}elseif($type == "array"){
$query = @mysqli_query($this->connection, $sql);
if($query){
return mysqli_fetch_array($query);
}else{
return $this->sqlError($sql, mysqli_error($this->connection));
}
}elseif($type == "assoc"){
$query = @mysqli_query($this->connection, $sql);
if($query){
return mysql_fetch_assoc($query);
}else{
return $this->sqlError($sql, mysqli_error($this->connection));
}
}elseif($type == "rows"){
$query = @mysqli_query($this->connection, $sql);
if($query){
return mysqli_num_rows($query);
}else{
return $this->sqlError($sql, mysqli_error($this->connection));
}
}
}
public function closeConnection(){
mysqli_close($this->connection);
}
}
$oDb = new database();
$oDb->connect();
if($oDb->tryLogin("Tom", "******") == 1){
echo 'Je hebt een verkeerd gebruikersnaam of wachtwoord ingevoerd!';
}else{
echo 'Je bent ingelogd!';
}
$rob = $oDb->userInfo("Rob", array("username, bedrag"));
echo '<br>'.$rob['username'].' zijn bedrag is '.$rob['bedrag'];
$oDb->query("UPDATE users SET bedrag='100' WHERE username='".$rob['username']."'");
echo '<br>Er zijn '.$oDb->query("SELECT id FROM users WHERE bedrag='100'", 'rows').' gebruikers met een bedrag van 100<br>';
$oDb->query("SELECT id FROM users WHEREhfghfghfg bedrag='25'", 'rows'); // Moet error geven
$oDb->closeConnection();
?>
class database{
private $db_server = "localhost";
private $db_user = "username";
private $db_pass = "*******";
private $db_database = "database";
public $connection;
public function connect(){
$this->connection = mysqli_connect($this->db_server, $this->db_user, $this->db_pass, $this->db_database);
}
public function userInfo($username, $columns=array()){
$items = '';
foreach($columns as $column){
$items .= $column.', ';
}
$items = substr($items, 0, -2);
$usersql = "SELECT $items FROM users WHERE username='".mysqli_real_escape_string($this->connection, $username)."'";
$usersel = mysqli_query($this->connection, $usersql);
return mysqli_fetch_assoc($usersel);
}
public function tryLogin($username, $wachtwoord){
$usersql = "SELECT id, username, password FROM users WHERE username='".mysqli_real_escape_string($this->connection, $username)."' AND password='".md5($wachtwoord)."'";
$usersel = mysqli_query($this->connection, $usersql);
$info = mysqli_fetch_assoc($usersel);
if($info['id'] == null){
return 1; // verkeerde inloggegevens
}else{
return 2; // ingelogd
}
}
private function sqlError($sql, $error){
echo '<span style="color: #FF0000; font-weight: bold;">Er is iets fout gegaan. Onze excuses hiervoor.<br>De foutmelding is automatisch verzonden en zal zo snel mogelijk worden opgelost.</span>';
mail("[email protected]", "sql error", 'SQL = '.$sql.'
mysqli_error = '.$error);
return true;
}
public function query($sql, $type=null){
if($type==null){
$query = @mysqli_query($this->connection, $sql);
if($query){
return $query;
}else{
return $this->sqlError($sql, mysqli_error($this->connection));
}
}elseif($type == "object"){
$query = @mysqli_query($this->connection, $sql);
if($query){
return mysqli_fetch_object($query);
}else{
return $this->sqlError($sql, mysqli_error($this->connection));
}
}elseif($type == "array"){
$query = @mysqli_query($this->connection, $sql);
if($query){
return mysqli_fetch_array($query);
}else{
return $this->sqlError($sql, mysqli_error($this->connection));
}
}elseif($type == "assoc"){
$query = @mysqli_query($this->connection, $sql);
if($query){
return mysql_fetch_assoc($query);
}else{
return $this->sqlError($sql, mysqli_error($this->connection));
}
}elseif($type == "rows"){
$query = @mysqli_query($this->connection, $sql);
if($query){
return mysqli_num_rows($query);
}else{
return $this->sqlError($sql, mysqli_error($this->connection));
}
}
}
public function closeConnection(){
mysqli_close($this->connection);
}
}
$oDb = new database();
$oDb->connect();
if($oDb->tryLogin("Tom", "******") == 1){
echo 'Je hebt een verkeerd gebruikersnaam of wachtwoord ingevoerd!';
}else{
echo 'Je bent ingelogd!';
}
$rob = $oDb->userInfo("Rob", array("username, bedrag"));
echo '<br>'.$rob['username'].' zijn bedrag is '.$rob['bedrag'];
$oDb->query("UPDATE users SET bedrag='100' WHERE username='".$rob['username']."'");
echo '<br>Er zijn '.$oDb->query("SELECT id FROM users WHERE bedrag='100'", 'rows').' gebruikers met een bedrag van 100<br>';
$oDb->query("SELECT id FROM users WHEREhfghfghfg bedrag='25'", 'rows'); // Moet error geven
$oDb->closeConnection();
?>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
<?php
class database{
private $db_server = "localhost";
private $db_user = "username";
private $db_pass = "*******";
private $db_database = "database";
public $connection;
public function connect(){
$this->connection = mysqli_connect($this->db_server, $this->db_user, $this->db_pass, $this->db_database);
}
}
?>
class database{
private $db_server = "localhost";
private $db_user = "username";
private $db_pass = "*******";
private $db_database = "database";
public $connection;
public function connect(){
$this->connection = mysqli_connect($this->db_server, $this->db_user, $this->db_pass, $this->db_database);
}
}
?>
Dat kan alvast een stuk netter:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Hetzelfde geldt voor andere methoden. Je class is nu een OOP-versie van de procedurele versie van MySQLi. Bijvoorbeeld:
Code (php)
Verder zou ik in jouw geval overwegen om MySQLi te extenden:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
class Database extends mysqli
{
const DB_SERVER = 'localhost';
const DB_USER = 'username';
const DB_PASS = '*******';
const DB_DATABASE = 'database';
public function __construct()
{
parent::__construct(self::DB_SERVER, self::DB_USER, self::DB_PASS, self::DB_DATABASE);
}
}
// Database is complete OOP-kloon van MySQLi en ondersteunt bijvoorbeeld:
$db = new Database();
$db->close();
?>
class Database extends mysqli
{
const DB_SERVER = 'localhost';
const DB_USER = 'username';
const DB_PASS = '*******';
const DB_DATABASE = 'database';
public function __construct()
{
parent::__construct(self::DB_SERVER, self::DB_USER, self::DB_PASS, self::DB_DATABASE);
}
}
// Database is complete OOP-kloon van MySQLi en ondersteunt bijvoorbeeld:
$db = new Database();
$db->close();
?>
Gewijzigd op 28/10/2013 12:46:38 door Ward van der Put