I am newbie to class and object.Here I have made some code for mysql database connection in class and object.in the index.php I have this code:
<?php
try {
require_once('./lib/testdb.php');
$login = new Login();
}
catch(Exception $error) {
print $error->getMessage();
}
/* Include the HTML for the form */
require_once('./lib/form.php');
?>
and for testdb.php I have this code:
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "root");
class Login{
private $username;
private $password;
public function getUsername() {
return $this->username;
}
public function getPassword() {
return $this->password;
}
public function getEncryptedPassword() {
return sha1($this->password);
}
public function connectToMySQL() {
@mysql_connect(DB_HOST, DB_USER, DB_PASS) OR die("Cannot connect to MySQL server!");
mysql_select_db("dd") OR die("Cannot select database!");
}
public function verifyLogin($username, $password) {
$this->username = $username;
if(empty($username) || empty($password)) {
throw new Exception(Login::ERROR_EMPTY_LOGIN);
}
else {
$query = sprintf("SELECT * FROM 'users' WHERE username = '%s' AND
userpass = sha1('%s') ");
$this->clean($username),
$this->clean($password));
$result = mysql_query($query) OR die('Cannot perform query!');
while($info = mysql_fetch_array( $result ))
{
echo $info['username'];
}
}
?>
The database part is like this:
--
-- Database: `login`
--
-- --------------------------------------------------------
--
-- Table structure for table `login`
--
CREATE TABLE IF NOT EXISTS `login` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(80) NOT NULL,
`password` varchar(80) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `login`
--
INSERT INTO `login` (`id`, `username`, `password`) VALUES
(1, 'ajay', 'password');
The problem is that it is not fetching any data or nothing from the database it is showing 'Cannot perform query'.So please guide me.This one is my first project in class and object.