Hi Folks,
I am trying to use a class for my database log in details in the OOP way, but when I instantiate the class in my work, I get an error message that 'No database was selected'. My code are pasted below and I thought that I have selected a database already, or am I doing something terribly wrong somewhere? I will appreciate your help please.
The connection class code
The test code file has the code below.Code:<?php class Connect { public $mysql; private $host = 'localhost'; private $username = 'root'; private $pwd = ''; private $database = 'students'; function __construct(){ $this->mysql = new mysqli($this->host, $this->username,$this->pwd,$this->database) or die(mysql_error()); echo "Successfully connected"; } function __destruct(){ //close mysql database connection $this->mysql->close(); } } ?>
I will appreciate some help please.Code:<?php include ('conn_class.php'); $Connect = new Connect(); ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Student Zone</title> <style type="text/css"> body {font-family: Arial, Helvetica, sans-serif;} #wrapper {width: 550px; height: auto; border: 1px solid #ccc; margin: 0px auto;} #toparea {height: 70px; border-bottom: 1px solid #ccc; font-size: 250%; padding-left: 10px; padding-top: 30px;} #content {height: auto;} #content p{margin: 0px; padding-left: 20px; padding-top: 15px;} #bottom {clear: both; border-top: 1px solid #ccc; height: 25px; text-align: center; font-size: 12px;} </style> </head> <body> <div id="wrapper"> <div id="toparea">Student Zone</div> <div id="centent"><p>Search a course below.</p> <?php $select = mysql_query("SELECT course_name FROM courses") or die(mysql_error()); while ($row = mysql_fetch_array($select)) { echo $row['course_name']; } ?> </div> <div id="bottom"><?php echo "©" . date('Y'); ?></div> </div> </body> </html>
Thanks,
Menre


Reply With Quote

