Gaat dit goed zo (Oefenen)
Er zijn al menig classes voor MySQL, maar het schijnt een goede oefening te zijn!
Dus bij deze! Gaat dit goed zo ?
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
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
<?php
error_reporting(E_ALL);
class dbConnection
{
// Basic Propertys
protected $host = 'localhost';
protected $user = 'laurens';
protected $pass = 'landstede110';
protected $db = 'test_db';
// Changeable propertys
protected $link_ID = 0;
protected $select_DB = 0;
protected $do_query = 0;
function __construct()
{
$this->link_ID = mysql_connect($this->host, $this->user, $this->pass);
if (!$this->link_ID)
{
return $this->trigger_db_error(mysql_error());
}
if ($this->link_ID)
{
$this->select_DB = mysql_select_db($this->db);
if (!$this->select_DB)
{
return $this->trigger_db_error(mysql_error());
}
}
}
function do_query($query)
{
$this->do_query = mysql_query($query);
if (!$this->do_query)
{
return $this->trigger_db_error(mysql_error());
}
while($row = mysql_fetch_array($this->do_query))
{
$Data[] = $row;
}
return $Data;
}
function trigger_db_error($error)
{
return sprintf('%s', $error);
}
}
$Connection = new dbConnection();
print_r($Connection->do_query("SELECT * FROM persons"));
?>
error_reporting(E_ALL);
class dbConnection
{
// Basic Propertys
protected $host = 'localhost';
protected $user = 'laurens';
protected $pass = 'landstede110';
protected $db = 'test_db';
// Changeable propertys
protected $link_ID = 0;
protected $select_DB = 0;
protected $do_query = 0;
function __construct()
{
$this->link_ID = mysql_connect($this->host, $this->user, $this->pass);
if (!$this->link_ID)
{
return $this->trigger_db_error(mysql_error());
}
if ($this->link_ID)
{
$this->select_DB = mysql_select_db($this->db);
if (!$this->select_DB)
{
return $this->trigger_db_error(mysql_error());
}
}
}
function do_query($query)
{
$this->do_query = mysql_query($query);
if (!$this->do_query)
{
return $this->trigger_db_error(mysql_error());
}
while($row = mysql_fetch_array($this->do_query))
{
$Data[] = $row;
}
return $Data;
}
function trigger_db_error($error)
{
return sprintf('%s', $error);
}
}
$Connection = new dbConnection();
print_r($Connection->do_query("SELECT * FROM persons"));
?>
Gewijzigd op 01/11/2010 11:35:00 door Laurens ten Ham
-Wat als je met meerdere db's wil verbinden? Kan nu alleen maar met eentje.
-Waar slaat die sprintf op bij je trigger_error?
-Waarom geen exceptions?
-Waarom alleen mysql_fetch_array, en dan geen mode meegeven?
Zo zou ik het doen, en dan waarschijnlijk nog met losse methodes (zodat je kan kiezen welk fetch-type je gebruikt, in losse methodes), want stel nu dat je een UPDATE doet. Fetchen heeft dan totaal geen zin...
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
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
<?php
ini_set("display_errors",1);
error_reporting(E_ALL);
class dbConnection
{
const DEFAULT_HOST = 'localhost';
const DEFAULT_USER = 'laurens';
const DEFAULT_PASS = 'landstede110';
const DEFAULT_DB = 'test_db';
protected $connection;
protected $last_result;
protected $last_data;
function __construct($host=null,$user=null,$pass=null,$db=null)
{
$this->host=(empty($host)?DEFAULT_HOST:$host);
$this->user=(empty($user)?DEFAULT_USER:$user);
$this->pass=(empty($pass)?DEFAULT_PASS:$pass);
$this->db=(empty($db)?DEFAULT_DB:$db);
$this->link_ID = mysql_connect($this->host, $this->user, $this->pass);
if (!$this->link_ID)
{
throw new dbConnectionException("Could not connect to database, error: " . mysql_error());
return;
}
if ( ! mysql_select_db($this->db) )
{
throw new dbConnectionException("Could not connect to database, error: " . mysql_error());
}
}
function do_query($query,$type=MYSQL_ASSOC)
{
$this->last_result = mysql_query($query);
if (!$this->last_result)
{
throw new dbConnectionException("Could not execute query, error: " . mysql_error());
return false;
}
$data=array();
while($row = mysql_fetch_array($this->do_query,$type))
{
$data[] = $row;
}
return $data;
}
/*function trigger_db_error($error)
{
throw new dbConnectionException($error);
}*/
}
class dbConnectionException extends Exception{}
try
{
$Connection = new dbConnection();
print_r($Connection->do_query("SELECT * FROM persons"));
}
catch(dbConnectionException $e)
{
echo $e->getMessage();
}
?>
ini_set("display_errors",1);
error_reporting(E_ALL);
class dbConnection
{
const DEFAULT_HOST = 'localhost';
const DEFAULT_USER = 'laurens';
const DEFAULT_PASS = 'landstede110';
const DEFAULT_DB = 'test_db';
protected $connection;
protected $last_result;
protected $last_data;
function __construct($host=null,$user=null,$pass=null,$db=null)
{
$this->host=(empty($host)?DEFAULT_HOST:$host);
$this->user=(empty($user)?DEFAULT_USER:$user);
$this->pass=(empty($pass)?DEFAULT_PASS:$pass);
$this->db=(empty($db)?DEFAULT_DB:$db);
$this->link_ID = mysql_connect($this->host, $this->user, $this->pass);
if (!$this->link_ID)
{
throw new dbConnectionException("Could not connect to database, error: " . mysql_error());
return;
}
if ( ! mysql_select_db($this->db) )
{
throw new dbConnectionException("Could not connect to database, error: " . mysql_error());
}
}
function do_query($query,$type=MYSQL_ASSOC)
{
$this->last_result = mysql_query($query);
if (!$this->last_result)
{
throw new dbConnectionException("Could not execute query, error: " . mysql_error());
return false;
}
$data=array();
while($row = mysql_fetch_array($this->do_query,$type))
{
$data[] = $row;
}
return $data;
}
/*function trigger_db_error($error)
{
throw new dbConnectionException($error);
}*/
}
class dbConnectionException extends Exception{}
try
{
$Connection = new dbConnection();
print_r($Connection->do_query("SELECT * FROM persons"));
}
catch(dbConnectionException $e)
{
echo $e->getMessage();
}
?>
Gewijzigd op 01/11/2010 15:21:21 door SilverWolf NL