[sluiten] PDO gebruiken in class

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

The Ultimate

The Ultimate

22/07/2013 14:48:46
Quote Anchor link
[UPDATE] Ik heb al een oplossing gevonden.

Ik probeer in mijn classes gebruik te maken van PDO connectie, maar krijg telkens deze foutmelding: Notice: Undefined property: Bootstrap::$db in E:\root\2013\AVDirect\libs\Question.php on line 32

Op regel 32 staat:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
$this->db->select("SELECT `q_id`, `q_sub`, `q_title`, `q_content`, `q_explanation`, `a_type` FROM `question` WHERE `q_id` = '$q_id'");


Bootstrap.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
<?php

class Bootstrap {

    function
__construct() {

        // Get URL pieces
        if(isset($_GET['url'])) {
            $url = Functions::getUrlPieces($_GET['url']);
        }
else {
            $url[0] = '';
        }


        // Include homepage if no specific page was requested
        if (empty($url[0])) {
            require 'pages/index.php';
            return false;
        }


        // If specific page was requested, check whether that page exists
        // If the page does exist, include the page
        // If the page does not exist, show errorpage.

        $file = 'pages/' . $url[0] . '.php';
        if (file_exists($file)) {
            require $file;
        }
else {
            require 'pages/404.php';
        }
    }
}

?>


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
<?php
$pdo
= new PDO('mysql:host=localhost;port=3307;dbname=' . DB_NAME, DB_USER, DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$result = $pdo->query('SELECT * FROM answer WHERE q_id = 1');
# Map results to object
$result->setFetchMode(PDO::FETCH_CLASS, 'Question');

while ($question = $result->fetch()) {
    # Call our custom full_name method
    echo $question->full_name();
}

?>


Question.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
<?php
class Question extends Database {

    public $db = "";
    public $q_id = "";

    public function __construct() {
        $this->db = new Database(DB_TYPE, DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASS);
    }

    
    public function full_name() {
        return 'Nummer: ' . $this->q_id;
    }


public function q_data_from_q_id($q_id) {
    $this->db->select("SELECT `q_id`, `q_sub`, `q_title`, `q_content`, `q_explanation`, `a_type` FROM `question` WHERE `q_id` = '$q_id'");
}

?>


Database.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
<?php
class Database extends PDO {

    public function __construct($DB_TYPE, $DB_HOST, $DB_PORT = 3307, $DB_NAME, $DB_USER, $DB_PASS) {
        parent::__construct($DB_TYPE . ':host=' . $DB_HOST . ';port=' . $DB_PORT . ';dbname=' . $DB_NAME, $DB_USER, $DB_PASS);

        //parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTIONS);
    }
    
    /**
     * select
     * @param string $sql An SQL string
     * @param array $array Parameters to bind
     * @param constant $fetchMode A PDO Fetch mode
     * @return mixed
     */

    public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC) {
        $sth = $this->prepare($sql);
        foreach ($array as $key => $value) {
            $sth->bindValue("$key", $value);
        }

        $sth->execute();
        return $sth->fetchAll($fetchMode);
    }
}

?>
Gewijzigd op 22/07/2013 15:40:02 door The Ultimate
 
Er zijn nog geen reacties op dit bericht.



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.