vreemde foutmeldingen met wederzijds gebruik van functies
Ik heb met volle overtuiging 3 klasses gecreeerd, waarvan de eerste 2 het alleen maar fatsoenlijk doen...
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
109
110
111
112
113
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
109
110
111
112
113
<?php
class SystemComponent
{
public $settings;
function getSettings()
{
// System variables
$settings['siteDir'] = $_SERVER['SCRIPT_FILENAME'];
// Database variables
$settings['dbhost'] = 'localhost';
$settings['dbusername'] = 'root';
$settings['dbpassword'] = 'pass';
return $settings;
}
}
class DbConnector
{
public $link;
//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector($db)
{
// Load settings from parent class
$settings = SystemComponent::getSettings();
// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
// Connect to the database
if ( ! $this->link = mysql_connect($host, $user, $pass))
{
trigger_error('Een error m.b.t. de MySQL Connectie: ' .mysql_error(),E_USER_ERROR);
}
mysql_select_db($db);
register_shutdown_function(array(&$this, 'close'));
}
//*** Function: query, Purpose: Execute a database query ***
function query($query)
{
if ( ! $query = mysql_query($query, $this->link) )
{
trigger_error('Een error m.b.t. de MySQL Query: ' .mysql_error(), E_USER_ERROR);
}
return $query;
}
//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result)
{
if ( $fetched = mysql_fetch_assoc($result) )
{
trigger_error('Een error m.b.t. het <i>fetchen</i> van het MySQL resultaat: ' .mysql_error(), E_USER_ERROR);
}
return $fetched;
}
//*** Function: close, Purpose: Close the connection ***
function close()
{
mysql_close($this->link);
}
}
class Menu
{
public function buildMenu($parentId, $menuData)
{
$connector = new DbConnector('mark moes');
// Use the query function of DbConnector to run a database query
$result = $connector->query("SELECT id, parentId, name FROM menu ORDER BY parentId, name");
// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
while ($menuItem = $connector->fetchArray($result))
{
$menuData['items'][$menuItem['id']] = $menuItem;
$menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}
$html = "";
if (isset($menuData['parents'][$parentId]))
{
$html = "<ul>\n";
foreach ($menuData['parents'][$parentId] as $itemId)
{
$html .= "<li>" . $menuData['items'][$itemId]['name'];
// find childitems recursively
$html .= $this->buildMenu($itemId, $menuData);
$html .= "</li>\n";
}
$html .= "</ul>\n";
}
return $html;
}
}
?>
class SystemComponent
{
public $settings;
function getSettings()
{
// System variables
$settings['siteDir'] = $_SERVER['SCRIPT_FILENAME'];
// Database variables
$settings['dbhost'] = 'localhost';
$settings['dbusername'] = 'root';
$settings['dbpassword'] = 'pass';
return $settings;
}
}
class DbConnector
{
public $link;
//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector($db)
{
// Load settings from parent class
$settings = SystemComponent::getSettings();
// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
// Connect to the database
if ( ! $this->link = mysql_connect($host, $user, $pass))
{
trigger_error('Een error m.b.t. de MySQL Connectie: ' .mysql_error(),E_USER_ERROR);
}
mysql_select_db($db);
register_shutdown_function(array(&$this, 'close'));
}
//*** Function: query, Purpose: Execute a database query ***
function query($query)
{
if ( ! $query = mysql_query($query, $this->link) )
{
trigger_error('Een error m.b.t. de MySQL Query: ' .mysql_error(), E_USER_ERROR);
}
return $query;
}
//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result)
{
if ( $fetched = mysql_fetch_assoc($result) )
{
trigger_error('Een error m.b.t. het <i>fetchen</i> van het MySQL resultaat: ' .mysql_error(), E_USER_ERROR);
}
return $fetched;
}
//*** Function: close, Purpose: Close the connection ***
function close()
{
mysql_close($this->link);
}
}
class Menu
{
public function buildMenu($parentId, $menuData)
{
$connector = new DbConnector('mark moes');
// Use the query function of DbConnector to run a database query
$result = $connector->query("SELECT id, parentId, name FROM menu ORDER BY parentId, name");
// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
while ($menuItem = $connector->fetchArray($result))
{
$menuData['items'][$menuItem['id']] = $menuItem;
$menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}
$html = "";
if (isset($menuData['parents'][$parentId]))
{
$html = "<ul>\n";
foreach ($menuData['parents'][$parentId] as $itemId)
{
$html .= "<li>" . $menuData['items'][$itemId]['name'];
// find childitems recursively
$html .= $this->buildMenu($itemId, $menuData);
$html .= "</li>\n";
}
$html .= "</ul>\n";
}
return $html;
}
}
?>
Wat ik nu wil doen is de class Menu met de functie buildMenu('0', 'blaat') uitvoeren. dit resulteert in de volgende foutmelding:
Code (php)
1
Fatal error: Een error m.b.t. het fetchen van het MySQL resultaat: in C:\xampp\htdocs\website\Class\Class.php on line 60
Hieruit maak ik op dat het fetchen dus niet is gelukt... Maar ik begrijp nog niet volledig waarom niet.
M.I. is er ergens iets fout gegaan met het heen en weer gebruiken van de $string's in OOP, maar ik kan nog niet helemaal volgen hoe het wel moet.
Kan iemand mij helpen?
Bij voorbaat bedankt
Gewijzigd op 01/01/1970 01:00:00 door Mark moes
Heb t script een beetje verbouwd: gezien vanaf regel 72:
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
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
<?php
class Menu
{
public $result;
public function buildMenu($parentId, $menuData)
{
$connector = new DbConnector('mark moes');
// Use the query function of DbConnector to run a database query
$this->result = $connector->query("SELECT id, parentId, name FROM menu ORDER BY parentId, name");
$fetch = $connector->fetchArray($this->result);
// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
while ($menuItem = $fetch)
{
$menuData['items'][$menuItem['id']] = $menuItem;
$menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}
?>
class Menu
{
public $result;
public function buildMenu($parentId, $menuData)
{
$connector = new DbConnector('mark moes');
// Use the query function of DbConnector to run a database query
$this->result = $connector->query("SELECT id, parentId, name FROM menu ORDER BY parentId, name");
$fetch = $connector->fetchArray($this->result);
// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
while ($menuItem = $fetch)
{
$menuData['items'][$menuItem['id']] = $menuItem;
$menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}
?>
Dit resulteert in deze foutmelding:
Code (php)
1
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 35 bytes) in C:\xampp\htdocs\website\Class\Class.php on line 97
En eigenlijk kan ik hier niks over vinden, houd dat in dat m'n cache volloopt??
edit
Ik heb de $fetch; geechoed, met resultaat "Array".... Maar als het een mysql resultaat betreft, moet die toch gewoon geechoed worden??
Edit 2
Er is wel een goede verbinding met de database, alleen het fetchen wil niet...
Gewijzigd op 01/01/1970 01:00:00 door mark moes
Alleen staan er nog veel meer resultaten in de db.
Waarom worden die niet geprint?
Ik kom er niet meer uit...
Deze code heb ik nu, volgens mij is dit technisch gezien ook een beetje meer OOP...
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
class SystemComponent
{
public $settings;
function getSettings()
{
// System variables
$settings['siteDir'] = $_SERVER['SCRIPT_FILENAME'];
// Database variables
$settings['dbhost'] = 'localhost';
$settings['dbusername'] = 'root';
$settings['dbpassword'] = '';
return $settings;
}
}
class DbConnect
{
public $link;
public $db;
public $result;
function __construct($db)
{
$this->db = $db;
}
//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector()
{
// Load settings from parent class
$settings = SystemComponent::getSettings();
// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
// Connect to the database
if ( ! $this->link = mysql_connect($host, $user, $pass))
{
trigger_error('Een error m.b.t. de MySQL Connectie: ' .mysql_error(),E_USER_ERROR);
}
mysql_select_db($this->db);
register_shutdown_function(array(@$this, 'close'));
}
//*** Function: query, Purpose: Execute a database query ***
function query($query)
{
if ( ! $query = mysql_query($query, $this->link) )
{
trigger_error('Een error m.b.t. de MySQL Query: ' .mysql_error(), E_USER_ERROR);
}
return $query;
}
//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result)
{
if ( ! $result = mysql_fetch_assoc($result) )
{
trigger_error('Een error m.b.t. het <i>fetchen</i> van het MySQL resultaat: ' .mysql_error(), E_USER_ERROR);
}
return $result;
}
//*** Function: close, Purpose: Close the connection ***
function close()
{
mysql_close($this->link);
}
}
class Menu
{
public $parentId;
public $fetch;
public function buildMenu($parentId, $menuData)
{
$connector = new DbConnect('mark moes'); //Select the db..
$connector->DbConnector(); // Make connection
$this->parentId = $parentId;
$this->fetch = $connector->fetchArray($connector->query('SELECT id, parentId, name FROM menu ORDER BY parentId, name'));
// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
print_r ($this->fetch);
while ($menuItem = $this->fetch)
{
$menuData['items'][$menuItem['id']] = $menuItem;
$menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}
$html = "";
if (isset($menuData['parents'][$this->parentId]))
{
$html = "<ul>\n";
foreach ($menuData['parents'][$this->parentId] as $itemId)
{
$html .= "<li>" . $menuData['items'][$itemId]['name'];
// find childitems recursively
$html .= $this->buildMenu($itemId, $menuData);
$html .= "</li>\n";
}
$html .= "</ul>\n";
}
return $html;
}
}
?>
class SystemComponent
{
public $settings;
function getSettings()
{
// System variables
$settings['siteDir'] = $_SERVER['SCRIPT_FILENAME'];
// Database variables
$settings['dbhost'] = 'localhost';
$settings['dbusername'] = 'root';
$settings['dbpassword'] = '';
return $settings;
}
}
class DbConnect
{
public $link;
public $db;
public $result;
function __construct($db)
{
$this->db = $db;
}
//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector()
{
// Load settings from parent class
$settings = SystemComponent::getSettings();
// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
// Connect to the database
if ( ! $this->link = mysql_connect($host, $user, $pass))
{
trigger_error('Een error m.b.t. de MySQL Connectie: ' .mysql_error(),E_USER_ERROR);
}
mysql_select_db($this->db);
register_shutdown_function(array(@$this, 'close'));
}
//*** Function: query, Purpose: Execute a database query ***
function query($query)
{
if ( ! $query = mysql_query($query, $this->link) )
{
trigger_error('Een error m.b.t. de MySQL Query: ' .mysql_error(), E_USER_ERROR);
}
return $query;
}
//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result)
{
if ( ! $result = mysql_fetch_assoc($result) )
{
trigger_error('Een error m.b.t. het <i>fetchen</i> van het MySQL resultaat: ' .mysql_error(), E_USER_ERROR);
}
return $result;
}
//*** Function: close, Purpose: Close the connection ***
function close()
{
mysql_close($this->link);
}
}
class Menu
{
public $parentId;
public $fetch;
public function buildMenu($parentId, $menuData)
{
$connector = new DbConnect('mark moes'); //Select the db..
$connector->DbConnector(); // Make connection
$this->parentId = $parentId;
$this->fetch = $connector->fetchArray($connector->query('SELECT id, parentId, name FROM menu ORDER BY parentId, name'));
// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
print_r ($this->fetch);
while ($menuItem = $this->fetch)
{
$menuData['items'][$menuItem['id']] = $menuItem;
$menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}
$html = "";
if (isset($menuData['parents'][$this->parentId]))
{
$html = "<ul>\n";
foreach ($menuData['parents'][$this->parentId] as $itemId)
{
$html .= "<li>" . $menuData['items'][$itemId]['name'];
// find childitems recursively
$html .= $this->buildMenu($itemId, $menuData);
$html .= "</li>\n";
}
$html .= "</ul>\n";
}
return $html;
}
}
?>
edit
verder krijg ik nog steeds de eerder geposte foutmelding.
Gewijzigd op 01/01/1970 01:00:00 door mark moes
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
// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
while ($menuItem = $connector->fetchArray($connector->query('SELECT id, parentId, name FROM menu ORDER BY parentId, name')) )
{
$menuData['items'][$menuItem['id']] = $menuItem;
$menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}
?>
// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
while ($menuItem = $connector->fetchArray($connector->query('SELECT id, parentId, name FROM menu ORDER BY parentId, name')) )
{
$menuData['items'][$menuItem['id']] = $menuItem;
$menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}
?>
Overigens krijg ik dan deze foutmelding:
Code (php)
1
Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\website\Class\Class.php on line 99
Ik heb het dus ook even geprobeert met de functie fetchArray() en dan niet fetch object maar weer array...
Code (php)
1
Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampp\htdocs\website\Class\Class.php on line 55
Bovenstaande foutmelding is daar het resultaat van. Ik heb zo kalm aan naar mijn weten lichtelijk alle dingen al geprobeert, maar ik ben nog vrij vers in dit onderwerp, dus ik denk dat ik iets over het hoofd zie.