DB Class word niet geinclude.

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Stefan Candan

Stefan Candan

10/07/2010 09:33:30
Quote Anchor link
Ik ben bezig met een site te maken dmv OOP PHP. Nu loop ik echter vast, omdat mijn database class niet functioneerd.

Index.php:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    
        <meta http-equiv="Content-Type" content="text/html;
        charset=iso-8859-1" />
        
        <title>
        
            OOP in PHP
            
        </title>
        
        <?php

            include("config.php");
            include("class_lib.php");
            
        ?>

    </head>
    
    <body>
    
        <?php
            
            echo "Query time: ".$db->GetDBTime() ."<br />";
            echo "Queries Executed: ".$db->getSqlCount() ."<br />";
            $stefan = new person();
            $stefan->set_name('Candan');
            $stefan->set_age(17);
            $stefan->set_race('Caucasian');
            $stefan->set_birthday('Dec-21-1992');
            $stefan->set_email('[email protected]');
            
            $stefan->get_data($stefan->name);
            
        ?>

    
    </body>
    
</html>


db_lib.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php

    class DBManager{

        var
$connection = '';
        var
$queryCounter = 0;
        var
$totalTime = 0;
        var
$errorCode = 0;
        var
$errorMsg = '';
        var
$resultSet = '';
        
        function
DBConnect($host, $user, $pass, $db) {
        
            $startTime = $this->getMicroTime();
            
            if(!$this->connection = @mysql_connect($host, $user, $pass, true)) {
            
                $this->errorCode = mysql_errno();
                $this->errorMsg = mysql_error();
                return false;
            
            }

            
            if(!@mysql_select_db($db, $this->connection)) {
            
                $this->errorCode = mysql_errno();
                $this->errorMsg = mysql_error();
                @
mysql_close($this->connection);
                return false;
            
            }

            
            $this->totalTime += $this->getMicroTime() - $startTime;
            
            return true;
                
        }
        
        function
executeQuery($sql) {
        
            $startTime = $this->getMicroTime();
            ++
$this->queryCounter;
            
            if(!$this->resultSet = @mysql_query($sql, $this->connection)) {
            
                $this->errorCode = mysql_errno();
                $this->errorMsg = mysql_error();
                $this->totalTime += $this->getMicroTime() - $startTime;
                return false;
                
            }

            
            $this->totalTime += $this->getMicroTime() - $startTime;
            return $this->resultSet;
            
        }
        
        function
getAffectedRows() {
        
            return @mysql_affect_rows($this->connection);
        
        }
        
        function
getSelectedRows() {
        
            return @mysql_num_rows($this->resultSet);
            
        }
        
        function
getInsertID() {
        
            return @mysql_insert_id($this->connection);
        
        }
        
        function
loadResult() {
        
            $array = array();
            while($row = mysql_fetch_object($this->resultSet)) {
            
                $array[] = $row;
            
            }

            
            mysql_free_result($this->resultSet);
            
            return $array;
        
        }

        function
getErrrorCode(){
        
            return $this->errorCode;
        
        }
    
        function
getErrorMessage(){
        
            return $this->errorMsg;
        
        }
        
        function
getDBTime() {
        
            return round($this->totalTime, 6);
        
        }
        
        function
getSqlcount() {
        
            return $this->queryCounter;
            
        }
        
        function
getMicroTime() {
        
            list($usec, $sec) = explode(" ", microtime());
            return ((float)$usec + (float)$sec);
        
        }
        
    }


?>


class_lib.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
    
    include("db_lib.php");
    include("config.php");
    
    $db = new DBManager();
    $db->DBConnect($_CONFIG['Host'], $_CONFIG['User'], $_CONFIG['Pass'], $_CONFIG['Name']);

    
    class person {
    
        var
$name;
        var
$age;
        var
$race;
        var
$birthday;
        var
$email;
                
        function
set_name($new) {
            
            $this->name = $new;
        
        }
        
        function
set_age($new) {
        
            $this->age = $new;
            
        }
        
        function
set_race($new) {
        
            $this->age = $new;
            
        }
        
        function
set_birthday($new) {
        
            $this->birthday = $new;
            
        }
        
        function
set_email($new) {
        
            $this->email = $new;
            
        }
        
        function
get_data($name) {
        
            $q = "SELECT * FROM persons WHERE name = '".$name."'";
            if(!$res = $db->executeQuery($q)) {
                echo "There was an error retrieving data from the database. <br />";
                echo "Error code: ".$db->getErrorCode .'<br />';
                echo "Error Message: ".$db->getErrorMessage .'<br />';
                echo "Query: ".$q;
            }
else {
                if($db->getSelectedRows($res) == 0) {
                    echo "No data found. <br /> Inserting data, Please be patient.<br />";
                    insert_data();
                    echo "Inserted data.";
                }
else {
                    echo "<pre>".print_r($res, true)."</pre>";
                }
            }
        }
        
        function
insert_data() {
        
            $q = "INSERT INTO persons (name, age, race, birthday, email) VALUES('".$this->name."', ".$this->age.", '".$this->race."', '".$this->birthday."', '".$this->email."')";
            if(!$res = $db->executeQuery($q)) {
                echo "There was an error insert data into the database. <br />";
                echo "Error code: ".$db->getErrorCode .'<br />';
                echo "Error Message: ".$db->getErrorMessage .'<br />';
                echo "Query: ".$q;
            }
else {
                echo "Query: ".$q."<br />";
                echo "Affected Rows: ".$db->getAffectedRows() ."<br />";
            }
        }
    }

    
?>


En in mijn config staat gewoon de mysql info.

Wat ik krijg op mijn index:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
Query time: 0.004515
Queries Executed: 0

Notice: Undefined variable: db in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\oop\class_lib.php on line 51

Fatal error: Call to a member function executeQuery() on a non-object in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\oop\class_lib.php on line 51
Gewijzigd op 10/07/2010 09:34:31 door Stefan Candan
 
PHP hulp

PHP hulp

29/03/2024 14:12:14
 
P Lekensteyn

P Lekensteyn

10/07/2010 10:11:07
Quote Anchor link
Kijk zelf op regel 51 in class_lib.php.
Dan zie je $db->executeQuery.
Wat zegt de foutmelding?
Je roept een functie aan op een niet-object (geen class dus).

Kijk je iets hoger, dan zie je dat PHP gelijk heeft.
Je hebt $db in de globale scope gedefinieerd, maar deze wordt niet autonatisch beschikbaar in de huidige scope.
Plaats nog even 'global $db;' op regel 49.

Je kunt beter een referentie naar de databaseklasse plaatsen in een member variable van Class person.
 
Stefan Candan

Stefan Candan

10/07/2010 16:54:07
Quote Anchor link
En hoe doe ik dat laatste? Als ik een var $db = new DBManager(); maak zegt hij dat new daar niet thuis hoort.

Ik doe het nu met de globals, maar 1 ding werkt niet.
De queryCounter werkt niet. Hij blijft op 0 staan, terwijl er toch echt wel een query word gemaakt.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.