PHP class question

I am trying to create a PHP class to validate username and password in MySql. I get "mysql_query (): the argument provided is not a valid MySQL-Link resource in D: \ 2010Portfolio \ football \ main.php on line 38" The database has errors: message .

When I move the userQuery code out of my class, it works fine. Only if it is inside my class. I'm not sure if the problem is that I am not creating a connection to mysql inside my class or not. Thanks for the help! Here is my code

 <?php 
    $userName=$_POST['userName'];
    $userPw=$_POST['password'];

     class checkUsers {
        public $userName;
        public $userPw;
      function checkUsers($userName='', $userPw=''){
      $this->userName=$userName;
      $this->userPw=$userPw;
      }
      function check1(){

        if (empty($this->userName) || empty($this->userPw)){

              $message="<strong>Please Enter Username and Password</strong>";

       }else{
//line 38 is next line  
      $userQuery=mysql_query("SELECT userName, userPw FROM user WHERE  

userName='$this->userName' and userPw='$this->userPw'", $connection);

                        if (!$userQuery){
              die("database has errors: ". mysql_error());
             }
           if(mysql_num_rows($userQuery)==0){
        $message="Please enter valid username and password";
        }   
         }//end empty check

        return $message;

       }//end check1 method

      }//end Class

      $checkUser=new checkUsers($userName, $userPw);
      echo $checkUser->check1();




      ?>

      

I appreciate any helps !!!!

+2


a source to share


2 answers


The problem is your method cannot see the connection object. Or use the global (

global $connection;

      



in your method definition (generally a bad idea) or try going to the singleton database connection (code samples can be found all over the internet).

+3


a source


It interprets $connection

as a local variable in your function check1()

.



Review the docs for the variable scope . You probably need to define global $connection

in your function.

+1


a source







All Articles