|
Categorieën >
Object georiënteerd
[OOPHP] Database connector (Opgelost)
Chilion – 15/03/2010 17:11 (Laatst gewijzigd op 15/03/2010 17:13)
|
|
offline
|
MySQL interesse
|
Beste mensen,
Ik ben redelijk nieuw in het OO fenomeen.
Er gaat hier iets fout, maar ik weet niet wat, wie kan mij even een zetje geven?
menu.php
<?php
require_once('database.php');
new Database();
class Menu
{
public $query = 'SELECT * FROM menu';
function __construct()
{
$result = mysqli_query($link, $this->query);
$menu = mysqli_fetch_array($res);
printr($menu);
}
}
new Menu();
?>
<?php require_once('database.php'); new Database(); class Menu { public $query = 'SELECT * FROM menu'; function __construct() { $result = mysqli_query($link, $this->query); $menu = mysqli_fetch_array($res); printr($menu); } } new Menu(); ?>
database.php
<?php
class Database
{
public $host = "localhost";
public $user = "chilion_oo";
public $pass = "*****";
public $base = "chilion_oo";
public $link = false;
function __construct()
{
$link = mysqli_connect($this->host, $this->user, $this->pass, $this->base);
if(!$link){
die('Could not connect to the database. Error:'.mysqli_error($link));
}
else {
print 'Succesfully connected to the database';
}
}
function disconnect()
{
mysql_close($link);
}
}
?>
<?php class Database { public $host = "localhost"; public $user = "chilion_oo"; public $pass = "*****"; public $base = "chilion_oo"; public $link = false; function __construct() { $link = mysqli_connect($this->host, $this->user, $this->pass, $this->base); if(!$link){ die('Could not connect to the database. Error:'.mysqli_error ($link)); } else { print 'Succesfully connected to the database'; } } function disconnect() { } } ?>
Foutmelding:
Citaat: Succesfully connected to the database
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/chilion/domains/chilion.nl/public_html/marc/inc/menu.php on line 14
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/chilion/domains/chilion.nl/public_html/marc/inc/menu.php on line 15
Fatal error: Call to undefined function printr() in /home/chilion/domains/chilion.nl/public_html/marc/inc/menu.php on line 17
Alvast bedankt! |
13 antwoorden
TotempaaltJ – 15/03/2010 17:12 (Laatst gewijzigd op 15/03/2010 17:13)
|
|
offline
|
PHP interesse
|
Wat gebeurt er wel dan? Zo kunnen we natuurlijk niks! Errors?
Overigens is het voor zover ik weet afgeraden om print's en echo's te doen in functies, gebruik liever returns!
|
|
|
|
offline
|
MySQL interesse
|
Ach ja, sorry. Ze staan erbij in
|
|
|
|
offline
|
PHP interesse
|
Hmm, de errors lijken me duidelijk, toch? De laatste nog wel het makkelijkst op te lossen: niet printr, PHP.net: print_r!
|
|
|
|
offline
|
MySQL interesse
|
Ja, dat snap ik ook, het ging eigenlijk om de bovenste 2 :/
En, ze zijn wel duidelijk ja, maar ik denk dat $link niet op die manier kan. $this->link werkt echter ook niet
Groetsels.
|
TotempaaltJ – 15/03/2010 17:22 (Laatst gewijzigd op 15/03/2010 17:36)
|
|
offline
|
PHP interesse
|
|
|
|
|
offline
|
MySQL interesse
|
Kun je misschien iets duidelijker zijn want ik snap de zak van je reacties
|
Raze – 15/03/2010 17:34 (Laatst gewijzigd op 15/03/2010 17:34)
|
|
offline
|
PHP beginner
|
|
|
|
|
offline
|
PHP gevorderde
|
<?php
require_once('database.php');
class Menu
{
public $query = 'SELECT * FROM menu';
private $_db;
function __construct()
{
$_db = new Database();
$result = mysqli_query($_db->link, $this->query);
$menu = mysqli_fetch_array($result);
print_r($menu);
}
}
new Menu();
?>
<?php require_once('database.php'); class Menu { public $query = 'SELECT * FROM menu'; private $_db; function __construct() { $_db = new Database(); $result = mysqli_query($_db->link, $this->query); $menu = mysqli_fetch_array($result); } } new Menu(); ?>
|
TotempaaltJ – 15/03/2010 17:35 (Laatst gewijzigd op 15/03/2010 17:37)
|
|
offline
|
PHP interesse
|
Srry, mijn schuld, negeer mijn reactie  En btw, lemoinet heeft het antwoord
|
|
|
|
offline
|
MySQL interesse
|
Toch krijg ik nog deze foutmelding.
Succesfully connected to the database
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/chilion/domains/chilion.nl/public_html/marc/inc/menu.php on line 14
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/chilion/domains/chilion.nl/public_html/marc/inc/menu.php on line 15
Succesfully connected to the database Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/chilion/domains/chilion.nl/public_html/marc/inc/menu.php on line 14 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/chilion/domains/chilion.nl/public_html/marc/inc/menu.php on line 15
|
Dit onderwerp is gesloten.
|
|
|