mysql-database-class
De onderstaande file is bijvoorbeeld sql.class.php, wel moet je zelf je gegeven van je database nog invoeren.
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
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
<?
class vraag_db {
//je gegevens van de DB hier onder invullen
var $DATABASE = '';
var $USERNAME = '';
var $PASSWORD = '';
var $SERVER = 'localhost';
// deze gegevens NIET veranderen
var $conn='';
var $CONNECTION='';
var $MSG = '';
###########################################
# Function: OpenConnection
# Description: connects to the database
###########################################
function open_db()
{
$conn = @mysql_connect($this->SERVER,$this->USERNAME,$this->PASSWORD);
if ((!$conn) || (!mysql_select_db($this->DATABASE,$conn)))
{
$this->MSG = "\r\n" . "Unable to connect to database - " . date('H:i:s');
echo $this->MSG;
return false;
}
else
{
$this->CONNECTION = $conn;
// $this->MSG = "\r\n" . "Open connection to database - " . date('H:i:s');
// echo $this->MSG;
return true;
}
}
############################
# Function: close database
############################
function close_db ()
{
if(mysql_close($this->CONNECTION))
{
//echo"sl";
}
}
#######################################################
# Function: vraag (Query array[row][fieldname/fieldindex])
#######################################################
function vraag($sql)
{
if(!$this->open_db())
{
return false;
exit;
}
if ((empty($sql)))
{
$this->MSG = "\r\n" . "SQL Statement is <code>null</code> or not a SELECT - " . date('H:i:s');
echo $this->MSG;
return false;
}
else
{
$results = @mysql_db_query($this->DATABASE,$sql);
if ((!$results) || (empty($results)))
{
$this->MSG = "\r\n" . mysql_error()." - " . date('H:i:s');
echo $this->MSG;
return false;
}
else
{
$i = 0;
$data = array();
while($row = mysql_fetch_array($results,MYSQL_ASSOC))
{
$ii = 0;
while(list($myVariableName,$sqlFieldName)=each($row))
{
$data[$i][$myVariableName] = $sqlFieldName;
} $i++;
$ii++;
}
mysql_free_result($results);
$this->close_db();
return $data;
}
}
}# einde Function vraag
}# einde class database
?>
class vraag_db {
//je gegevens van de DB hier onder invullen
var $DATABASE = '';
var $USERNAME = '';
var $PASSWORD = '';
var $SERVER = 'localhost';
// deze gegevens NIET veranderen
var $conn='';
var $CONNECTION='';
var $MSG = '';
###########################################
# Function: OpenConnection
# Description: connects to the database
###########################################
function open_db()
{
$conn = @mysql_connect($this->SERVER,$this->USERNAME,$this->PASSWORD);
if ((!$conn) || (!mysql_select_db($this->DATABASE,$conn)))
{
$this->MSG = "\r\n" . "Unable to connect to database - " . date('H:i:s');
echo $this->MSG;
return false;
}
else
{
$this->CONNECTION = $conn;
// $this->MSG = "\r\n" . "Open connection to database - " . date('H:i:s');
// echo $this->MSG;
return true;
}
}
############################
# Function: close database
############################
function close_db ()
{
if(mysql_close($this->CONNECTION))
{
//echo"sl";
}
}
#######################################################
# Function: vraag (Query array[row][fieldname/fieldindex])
#######################################################
function vraag($sql)
{
if(!$this->open_db())
{
return false;
exit;
}
if ((empty($sql)))
{
$this->MSG = "\r\n" . "SQL Statement is <code>null</code> or not a SELECT - " . date('H:i:s');
echo $this->MSG;
return false;
}
else
{
$results = @mysql_db_query($this->DATABASE,$sql);
if ((!$results) || (empty($results)))
{
$this->MSG = "\r\n" . mysql_error()." - " . date('H:i:s');
echo $this->MSG;
return false;
}
else
{
$i = 0;
$data = array();
while($row = mysql_fetch_array($results,MYSQL_ASSOC))
{
$ii = 0;
while(list($myVariableName,$sqlFieldName)=each($row))
{
$data[$i][$myVariableName] = $sqlFieldName;
} $i++;
$ii++;
}
mysql_free_result($results);
$this->close_db();
return $data;
}
}
}# einde Function vraag
}# einde class database
?>