开发者

class problem in php

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 A开发者_如何学编程UTO_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.


You have an error in your sprintf statement:

$query = sprintf("SELECT * FROM 'users' WHERE username = '%s' AND 
        userpass = sha1('%s') ");    // here
        $this->clean($username),
        $this->clean($password));

should be:

$query = sprintf("SELECT * FROM 'users' WHERE username = '%s' AND 
        userpass = sha1('%s') ",    // corrected
        $this->clean($username),
        $this->clean($password));

As a result of that error, you are not adding your username and password to the query.


the class must be construct using parentheses: new Login(); not new Login; :

<?php 

  try {

    require_once('./lib/testdb.php');

    $login = new Login();

  }


Use mysql_error() to find out what the real problem is.

$result = mysql_query($query) OR die('Cannot perform query: ' . mysql_error());  


Retain some sanity and productivity and start looking at the last line in your mysql log file for these kinds of simple errors.

Alternatively, in order to temporarily id what the problem is simply

echo $query;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜