1

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.

1
  • Where is $login->verifyLogin() being called from here? Commented May 23, 2011 at 17:37

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

But this error should throw a syntax error and not the error which was mentioned. Maybe this was just a copy paste error when adding the code to the question.
that should throw the error but it is not showing any thing like that.olease helpme out to solve this problem
@kayahr That's true; without the actual code there's nothing anybody can do. @user614208 Please post the actual code and do a var_dump($query), you probably have a syntax error in your sql statement.
0

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

<?php 

  try {

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

    $login = new Login();

  }

Comments

0

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

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

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.