[PHP][OOP]class werkt niet

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • Spruit_elf
  • Registratie: Februari 2001
  • Laatst online: 10-08 01:09

Spruit_elf

Intentionally left blank

Topicstarter
Ik ben een beetje aan het expirimenteren met OOP en php ed en nu probeerde ik een database class werkend te krijgen

mysqldb_obj_inc.php
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
<?php
class mysqldb {

    //  set up the object
    var $host;
    var $db;
    var $user;
    var $password;
    var $sql;
    var $numrows;
    var $connection;
    
    //  Property Get & Set
    function gethost() {
        return $this->host;
    }
    function sethost($req_host) {
        $this->host = $req_host;
    // ff geknipt in de Get & Set functies omdat ze allemaal ongeveer het zelfde zijn als bovenstaande

    //  Magic functions
    function mysqldb() {
        global $mysqlhost, $mysqldatabase, $mysqlusername, $mysqlpassword;
        $this->sethost($mysqlhost);
        $this->setdb($mysqldatabase);
        $this->setuser($mysqlusername);
        $this->setpassword($mysqlpassword);
        $this->setconnection(0);
    }
    //  Methods
    function openconnection() {
        $this->dblink = mysql_connect($this->host, $this->user, $this->password);
        if ($this->connection == 1) {
            $this->db = mysql_select_db($this->db);
            $this->setconnection(1);
        } else {
            $this->setconnection(0);
            return false;
        }
        return true;
    }
    function closeconnection() {
        if ($this->dbconnection = 1) {
            mysql_close($this->dblink);
        }
    }
    function selectquery() {
        if ($this->connection == 0)
            $this->openconnection();
        $this->qry = mysql_query($this->sql);
        if (!$this->qry) {
            echo('[Error:] mysql_query failed');
            return false;
        } else {
            $this->numrows = mysql_num_rows($this->qry);
            if ($this->numrows > 0) {
                for($x = 0; $x > $this->numrows; $x++) {
                    $this->result[$x] = mysql_fetch_array($this->qry);
                }
            } else {
                echo('[Error:] mysql_num_rows failed');
                return false;
            }
            return true;
        }
    }
}
?>


dit is de betreffende class die word gebruikt in

test.php
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?
include('include/mysqldb_obj_inc.php');
include('include/conf_inc.php');
$db0 = new mysqldb();
$db0->openconnection();
$db0->setsql("SELECT nid,title FROM news_news ORDER BY nid ASC");
$db0->selectquery();
for ($x = 0; $x < count($db0->result); $x++) {
    echo('<li><a href="?page=home#nid' . $db0->result[$x]['nid'] . '">' . $db0->result[$x]['title'] . '</a></li>');
}
    
echo($db0->result[1]->title);
echo($db0->connection);
echo($db0->result);
echo($db0->qry);
?>


het enige wat ik hier uit krijg is [Error:] mysql_query failed, toch kan ik niet vinden waarom die failed voor zover ik kan zien klopt alles
ben al een hele dag bezig om uit te vinden wat er niet klopt en of alle variabelen wel kloppen, vandaar die echo's aan het eind.

het ligt niet aan de database, die klopt en werkt en daar zit info in.
kan iemand me verder helpen?

Those who danced were thought to be quite insane by those who could not hear the music.


Acties:
  • 0 Henk 'm!

  • elevator
  • Registratie: December 2001
  • Niet online

elevator

Officieel moto fan :)

Voeg even mysql_error() toe en print je SQL string uit in de "echo mysql_failed" functie - dan heb je denk ik een beter inzicht in wat er mis gaat :)

Acties:
  • 0 Henk 'm!

  • PrisonerOfPain
  • Registratie: Januari 2003
  • Laatst online: 26-05 17:08
Waar heb je setsql gedefinieerd?

Acties:
  • 0 Henk 'm!

  • Spruit_elf
  • Registratie: Februari 2001
  • Laatst online: 10-08 01:09

Spruit_elf

Intentionally left blank

Topicstarter
elevator schreef op 25 juli 2004 @ 18:33:
Voeg even mysql_error() toe en print je SQL string uit in de "echo mysql_failed" functie - dan heb je denk ik een beter inzicht in wat er mis gaat :)
1046: No Database Selected is wat hij geeft al ik
PHP:
1
echo mysql_errno($this->dblink) . ': ' . mysql_error($this->dblink);

achter de foutmeldingen zet heel vreemd want zover ik kan zien klopt het wel in dat gedeelte
aangezien echo($db0->db); wel de goede database geeft
* Spruit_elf gaat weer ff testen
PrisonerOfPain schreef op 25 juli 2004 @ 18:35:
Waar heb je setsql gedefinieerd?
zie comment in get set deel, die heb ik geknipt, hij werkt precies als sethost

PHP:
1
2
3
    function setsql($req_sql) {
        $this->sql = $req_sql;
    }

[ Voor 54% gewijzigd door Spruit_elf op 25-07-2004 18:55 ]

Those who danced were thought to be quite insane by those who could not hear the music.


Acties:
  • 0 Henk 'm!

Verwijderd

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
//    Methods 
    function openconnection() { 
        $this->dblink = mysql_connect($this->host, $this->user, $this->password); 
        if ($this->connection == 1) { 
            $this->db = mysql_select_db($this->db); 
            $this->setconnection(1); 
        } else { 
            $this->setconnection(0); 
            return false; 
        } 
        return true; 
    }


Connection heeft in eerste iig niet de waarde 1. Ofwel je maakt connectie met een database server en omdat connection geen 1 is selecteer je nooit een database.

Vervang $this->connection == 1 eens met $this->dblink. Op die manier kijk of er een geldige mysql connectie tot stand is gekomen. Dan zou alles moeten werken.

Acties:
  • 0 Henk 'm!

  • Spruit_elf
  • Registratie: Februari 2001
  • Laatst online: 10-08 01:09

Spruit_elf

Intentionally left blank

Topicstarter
ik heb idd ff de open en close connection functies iets beter gemaakt

PHP:
1
2
3
4
5
6
7
8
9
10
11
    function openconnection() {
        $this->dblink = mysql_connect($this->host, $this->user, $this->password)
            or die ('[Error:] mysql_connect failed<br />' . mysql_errno($this->dblink) . ': ' . mysql_error($this->dblink) . '<br />');
        mysql_select_db($this->db)
            or die ('[Error:] mysql_select_db failed<br />' . mysql_errno($this->dblink) . ': ' . mysql_error($this->dblink). '<br />');
        return true;
    }
    function closeconnection() {
        mysql_close($this->dblink);
        return true;
    }

nu geeft ie aleen helemaal niets meer, zelfs niet een fout melding

[ Voor 16% gewijzigd door Spruit_elf op 25-07-2004 19:26 ]

Those who danced were thought to be quite insane by those who could not hear the music.


Acties:
  • 0 Henk 'm!

  • Apollo_Futurae
  • Registratie: November 2000
  • Niet online
PHP:
1
2
3
4
5
6
7
8
9
10
11
    function openconnection() {
        $this->dblink = mysql_connect($this->host, $this->user, $this->password);
        if ($this->connection == 1) {
            $this->db = mysql_select_db($this->db);
            $this->setconnection(1);
        } else {
            $this->setconnection(0);
            return false;
        }
        return true;
    }

Als ik het goed begrijp doet deze functie het volgende:
Maak een verbinding met de database.
Kijk vervolgens of er al een verbinding was (dat is toch wat $this->connection aangeeft?); als dat zo is, selecteer dan de juiste database en zet connection op 1 (maar die stond daar al op :?); als dat niet zo is, zet dan connection op 0 (maar laat de verbinding intact, zonder een database te selecteren).

Trouwens, mysql_select_db geeft true terug als het selecteren gelukt is en anders false. Is het de bedoeling dat dat wordt opgeslagen in $this->db? Het lijkt me niet.

Waarschijnlijk gebeurt in test.php dit:
Je roept openconnection aan. De verbinding wordt gemaakt, maar $this->connection staat nog op 0 (zo is hij geïnitialiseerd), dus wordt er geen database geselecteerd en wordt $this->connection op 0 gezet.
Later roep je selectquery aan. Deze ziet dat $this->connection op 0 nul staat en roept daarom nogmaals openconnection aan, maar dat doet precies hetzelfde als hierboven: er wordt een verbinding gemaakt, maar geen database geselecteerd. Nu voert selectquery de ingestelde query uit. Dit veroorzaakt een fout, want er is geen database ingesteld.

Als je er nu eens zoiets van maakt:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    function openconnection() {
        if ($this->getconnection()) {
            $this->closeconnection();
        }
        $this->dblink = mysql_connect($this->host, $this->user, $this->password);
        if ($this->dblink !== false) {
            if (mysql_select_db($this->db)) {
                $this->setconnection(true);
            } else {
                echo(mysql_error());
                $this->setconnection(false);
            }
        } else {
            echo(mysql_error());
            $this->setconnection(false);
        }
        return($this->getconnection());
    }


Nog over deze functie:
PHP:
1
2
3
4
5
    function closeconnection() {
        if ($this->dbconnection = 1) {
            mysql_close($this->dblink);
        }
    }

Ik denk dat dit moet zijn:
PHP:
1
2
3
4
5
6
    function closeconnection() {
        if ($this->getconnection()) {
            mysql_close($this->dblink);
            $this->setconnection(false);
        }
    }


Beetje laat B)

[ Voor 8% gewijzigd door Apollo_Futurae op 25-07-2004 19:52 . Reden: Beetje laat B) ]

Pas de replâtrage, la structure est pourrie.


Acties:
  • 0 Henk 'm!

  • Apollo_Futurae
  • Registratie: November 2000
  • Niet online
mrcactus schreef op 25 juli 2004 @ 19:24:
PHP:
1
2
$this->dblink = mysql_connect($this->host, $this->user, $this->password)
            or die ('[Error:] mysql_connect failed<br />' . mysql_errno($this->dblink) . ': ' . mysql_error($this->dblink) . '<br />');
Je gebruikt nu $this->dblink (nl. in mysql_errno en mysql_error) voordat er iets aan is toegewezen.

$this->dblink is trouwens niet gedeclareerd.

Heb je ergens een error_reporting(E_ALL) staan (of vergelijkbaar)?

Pas de replâtrage, la structure est pourrie.


Acties:
  • 0 Henk 'm!

  • Spruit_elf
  • Registratie: Februari 2001
  • Laatst online: 10-08 01:09

Spruit_elf

Intentionally left blank

Topicstarter
ok heb heb het nu veranderd in dit

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
<?php
class mysqldb {

    //  set up the object
    var $host;
    var $db;
    var $user;
    var $password;
    var $sql;
    var $numrows;
    var $dblink;
    var $result;
    var $qry;
    
    //  Property Get & Set
    function gethost() {
        return $this->host;
    }
    function sethost($req_host) {
        $this->host = $req_host;
    }
// ff geknipt in de Get & Set functies omdat ze allemaal ongeveer het zelfde zijn als bovenstaande,
// alle vars hebben een get en set functie zoals bovenstaande

    
    
    //  Magic functions
    function mysqldb() {
        global $mysqlhost, $mysqldatabase, $mysqlusername, $mysqlpassword;
        $this->sethost($mysqlhost);
        $this->setdb($mysqldatabase);
        $this->setuser($mysqlusername);
        $this->setpassword($mysqlpassword);
    }
    //  Methods
    function openconnection() {
        if ($this->dblink) {
            return true;
        } else {
            $this->dblink = mysql_connect($this->host, $this->user, $this->password);
            if ($this->dblink) {
                mysql_select_db($this->db)
                or die ('[Error:] mysql_select_db failed<br />' . mysql_errno($this->dblink) . ': ' . mysql_error($this->dblink). '<br />');
            } else { 
                echo('[Error:] mysql_connect failed<br />' . mysql_errno($this->dblink) . ': ' . mysql_error($this->dblink) . '<br />');
            }
        }
    }
    function closeconnection() {
        if (!$this->dblink) {
            mysql_close($this->dblink);
        } else {
            echo('[Error:] Connection already closed');
        }
        return true;
    }
    function selectquery() {
        if ($this->dblink)
            $this->openconnection();
        $this->qry = mysql_query($this->sql);
        if (!$this->qry) {
            echo('[Error:] mysql_query failed' . mysql_errno($this->dblink) . ': ' . mysql_error($this->dblink). '<br />');
            return false;
        } else {
            $this->numrows = mysql_num_rows($this->qry);
            if ($this->numrows > 0) {
                for($x = 0; $x > $this->numrows; $x++) {
                    $this->result[$x] = mysql_fetch_array($this->qry);
                    echo('a');
                }
                echo('b');
            } else {
                echo('[Error:] mysql_num_rows failed<br />' . mysql_errno($this->dblink) . ': ' . mysql_error($this->dblink). '<br />');
                return false;
            }
            return true;
        }
    }
}
?>


en

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?
error_reporting(E_ALL);
include('include/mysqldb_obj_inc.php');
include('include/conf_inc.php');
$db0 = new mysqldb();
$db0->openconnection();
$db0->setsql("SELECT nid,title FROM news_news ORDER BY nid ASC");
$db0->selectquery();
for ($x = 0; $x < count($db0->result); $x++) {
    echo('<li><a href="?page=home#nid' . $db0->result[$x]['nid'] . '">' .     $db0->result[$x]['title'] . '</a></li>');
}
echo(implode(',',get_object_vars($db0)));
?>

de complete output is nu

blocalhost,database,user,password,SELECT nid,title FROM news_news ORDER BY nid ASC,2,Resource id #4,,Resource id #6

database user en password zijn, offcourse ff vranderd ;)

de eerste b voor localhost hoort niet bij de var maar die komt van de echo('b'); die tussen de selectquery staat

het lijkt mij nu alsof php de for loop overslaat, maar ik zie geen reden waarom die dat zou doen

[ Voor 38% gewijzigd door Spruit_elf op 25-07-2004 20:20 ]

Those who danced were thought to be quite insane by those who could not hear the music.


Acties:
  • 0 Henk 'm!

  • PrisonerOfPain
  • Registratie: Januari 2003
  • Laatst online: 26-05 17:08
$x > $this->numrows zal de eerste keer al false opleveren omdat $x 0 is, $x < $this->numrows niet ;)

Acties:
  • 0 Henk 'm!

  • Spruit_elf
  • Registratie: Februari 2001
  • Laatst online: 10-08 01:09

Spruit_elf

Intentionally left blank

Topicstarter
....
* Spruit_elf krijgt rode kleur
damn wat een klote fout
tnx nu werkt het

Those who danced were thought to be quite insane by those who could not hear the music.


Acties:
  • 0 Henk 'm!

  • Apollo_Futurae
  • Registratie: November 2000
  • Niet online
mrcactus schreef op 25 juli 2004 @ 20:17:
PHP:
1
2
3
4
5
6
7
8
9
10
11
$this->numrows = mysql_num_rows($this->qry);
if ($this->numrows > 0) {
    for($x = 0; $x > $this->numrows; $x++) {
        $this->result[$x] = mysql_fetch_array($this->qry);
        echo('a');
    }
    echo('b');
} else {
    echo('[Error:] mysql_num_rows failed<br />' . mysql_errno($this->dblink) . ': ' . mysql_error($this->dblink). '<br />');
    return false;
}
Als mysql_num_rows 0 geeft, betekent dat dat de query nul rijen opgeleverd heeft, niet dat er iets mis is gegaan.

Je zou het ook zo kunnen doen:
PHP:
1
2
3
4
$this->result = array();
while (false !== ($row = mysql_fetch_array($this->dblink))) {
    $this->result[] = $row;
}

offtopic:
Als je getters en setters niet gebruikt, hoef je ze ook niet te maken ;).

Pas de replâtrage, la structure est pourrie.

Pagina: 1