mysql-class-voor-php-5
Gesponsorde koppelingen
PHP script bestanden
common.php
-----------------------
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
if(!defined('IN_SITE'))
{
die();
}
if(!file_exists('config.php'))
{
die('De config file kon niet worden gevonden.');
}
require('config.php');
require('includes/sql.php');
define('DEBUG',$debug);
if(DEBUG)
{
ini_set(error_reporting, 1);
error_reporting(E_ALL);
}
?>
if(!defined('IN_SITE'))
{
die();
}
if(!file_exists('config.php'))
{
die('De config file kon niet worden gevonden.');
}
require('config.php');
require('includes/sql.php');
define('DEBUG',$debug);
if(DEBUG)
{
ini_set(error_reporting, 1);
error_reporting(E_ALL);
}
?>
config.php
Code (php)
includes/sql.php
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
if(!defined('IN_SITE'))
{
die();
}
/***
* Data beveiligen
**/
function secure($data)
{
if (get_magic_quotes_gpc())
{
$data = stripslashes($data);
}
if (!is_numeric($data)) {
$data = '\'' . mysql_real_escape_string($data) . '\'';
}
return $data;
}
class sql
{
// vul in deze 4 velden verkeerde gegevens in om een error op te roepen.
private $localhost = 'localhost';
private $username = ''; // username
private $password = ''; // password
private $database = ''; // database
private $connection = '';
private $message = '';
private $error = '';
/***
* Connectie beginnen
**/
public function __construct()
{
$connected = @mysql_connect($this->localhost,$this->username,$this->password);
if($connected and @mysql_select_db($this->database))
{
$this->connection = $connected;
}
else
{
$this->error .= 'Fout: ' . mysql_error();
$this->message .= 'Er is geen connectie met de database gemaakt.';
throw new sqlException($this->error,$this->message);
}
}
/***
* Query opbouwen en resultaat ophalen
**/
public function query($data)
{
if($this->connection)
{
$result = mysql_query($data);
if($result)
{
return $result;
}
else
{
$this->error .= 'Fout: ' . mysql_error();
$this->message .= 'Er zit een fout in de query, query gestopt.';
throw new sqlException($this->error,$this->message);
}
}
else
{
$this->error .= 'Fout: ' . mysql_error();
$this->message .= 'Er is geen connectie met de database gemaakt.';
throw new sqlException($this->error,$this->message);
}
}
}
/***
* Error message opbouwen. DEBUG mode geeft ook errors aan.
**/
class sqlException extends Exception
{
protected $error;
public function __construct($error, $message, $code = 0)
{
parent::__construct($message, $code);
$this->error = $error;
}
public function getError()
{
if(DEBUG)
{
return $this->error."\n<br />".$this->getMessage();
}
else
{
return $this->getMessage();
}
}
}
?>
if(!defined('IN_SITE'))
{
die();
}
/***
* Data beveiligen
**/
function secure($data)
{
if (get_magic_quotes_gpc())
{
$data = stripslashes($data);
}
if (!is_numeric($data)) {
$data = '\'' . mysql_real_escape_string($data) . '\'';
}
return $data;
}
class sql
{
// vul in deze 4 velden verkeerde gegevens in om een error op te roepen.
private $localhost = 'localhost';
private $username = ''; // username
private $password = ''; // password
private $database = ''; // database
private $connection = '';
private $message = '';
private $error = '';
/***
* Connectie beginnen
**/
public function __construct()
{
$connected = @mysql_connect($this->localhost,$this->username,$this->password);
if($connected and @mysql_select_db($this->database))
{
$this->connection = $connected;
}
else
{
$this->error .= 'Fout: ' . mysql_error();
$this->message .= 'Er is geen connectie met de database gemaakt.';
throw new sqlException($this->error,$this->message);
}
}
/***
* Query opbouwen en resultaat ophalen
**/
public function query($data)
{
if($this->connection)
{
$result = mysql_query($data);
if($result)
{
return $result;
}
else
{
$this->error .= 'Fout: ' . mysql_error();
$this->message .= 'Er zit een fout in de query, query gestopt.';
throw new sqlException($this->error,$this->message);
}
}
else
{
$this->error .= 'Fout: ' . mysql_error();
$this->message .= 'Er is geen connectie met de database gemaakt.';
throw new sqlException($this->error,$this->message);
}
}
}
/***
* Error message opbouwen. DEBUG mode geeft ook errors aan.
**/
class sqlException extends Exception
{
protected $error;
public function __construct($error, $message, $code = 0)
{
parent::__construct($message, $code);
$this->error = $error;
}
public function getError()
{
if(DEBUG)
{
return $this->error."\n<br />".$this->getMessage();
}
else
{
return $this->getMessage();
}
}
}
?>
index.php
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
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
<?php
// zorg ervoor dat IN_SITE waar is.
define('IN_SITE',true);
// gegevens inladen
require('common.php');
try
{
// class oproepen
$sql = new sql;
// data beveiligen met mysql_real_escape_String
$username = secure($_POST['username']);
// de query met de class
$result = $sql->query("SELECT * FROM testtabel WHERE username=".$username."");
while($row = mysql_fetch_assoc($result))
{
echo $row['name']."<br />\n";
}
}
catch(sqlException $e)
{
echo 'Er is een onverwachte fout opgetreden :'."<br /> ---- \n <br />";
echo $e->getError();
}
if(DEBUG)
{
echo "<br />\n <br />".'Debug mode staat aan.';
}
else
{
echo "<br />\n <br />".'Debug mode staat uit';
}
?>
// zorg ervoor dat IN_SITE waar is.
define('IN_SITE',true);
// gegevens inladen
require('common.php');
try
{
// class oproepen
$sql = new sql;
// data beveiligen met mysql_real_escape_String
$username = secure($_POST['username']);
// de query met de class
$result = $sql->query("SELECT * FROM testtabel WHERE username=".$username."");
while($row = mysql_fetch_assoc($result))
{
echo $row['name']."<br />\n";
}
}
catch(sqlException $e)
{
echo 'Er is een onverwachte fout opgetreden :'."<br /> ---- \n <br />";
echo $e->getError();
}
if(DEBUG)
{
echo "<br />\n <br />".'Debug mode staat aan.';
}
else
{
echo "<br />\n <br />".'Debug mode staat uit';
}
?>