Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5961387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:57:28+00:00 2026-05-22T18:57:28+00:00

I am newbie to class and object.Here I have made some code for mysql

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:57:28+00:00Added an answer on May 22, 2026 at 6:57 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Complete C# newbie here, so cut me some slack. I have this application that
Python and django newbie question, here is code: class Client(User) #some fields client=Client() client.save()
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I am newbie to ajax here and I know somebody would have encountered this
What's wrong with this Groovy code? class Pretending { public int count=0 Object get(String
i'm a newbie here and in GWT. Well, i have some questions with CellTable...
Newbie here, trying to get this to work: I am making an online index
Newbie Python question. I have a class that inherits from several classes, and some
Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
I am a Python newbie. I have this small problem. I want to print

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.