-
OOP error
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
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();
}
}
?>
The test code file has the code below.
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>
I will appreciate some help please.
Thanks,
Menre
-
Re: OOP error
In your code, you are using mysql_error() function, that is you are mixing up the legacy mysql function with mysqli. When creating an object of mysqli, it would return an object.
Check this example: http://www.php.net/manual/en/mysqli....p#example-1625
:wave:
-
Re: OOP error
Thanks for your response. I tried following your advice and doing it the way they suggested, but got the same error message 'No database selected'. I even tried copying and modifying their code to the one below and the error message was the same. But when I use the procedural style to connect, everything works fine.
Code:
<?php
class Connect extends mysqli {
public function __construct($localhost, $root, $pass, $db) {
parent::__construct($localhost, $root, $pass, $db);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
}
}
$db = new Connect('localhost', 'root', '', 'students');
echo 'Success... ' . $db->host_info . "\n";
$db->close();
?>
Or am I still doing something wrong?
Thanks,
Menre
-
Re: OOP error
When you call mysql_connect and mysql_select_db, the connection is persisted for all mysql_* functions.
When you create a mysqli object, the connection belongs to that object only.
So this is wrong:
PHP Code:
$db = new mysqli(...);
mysql_query(......)
This is right:
PHP Code:
$db = new mysqli(...);
$db->query(.......)
The class you've created doesn't really add any value (unless you've omitted most of it for the sake of this post). It actually makes your code more cumbersome because you've hidden the mysqli object within a class without exposing any methods for actually querying the database.
-
Re: OOP error
I agree with you that the class doesn't actually add any value to the code. I just prefer a simple way of making the MySQL database connection.