views:

49

answers:

2
+1  Q: 

PHP Class Question

Hi all I am trying to build a PHP class to check the username and password in MySql. I am getting "mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\2010Portfolio\football\main.php on line 38" database has errors:" message.

When I move my userQuery code out of my Class, it works fine. Only if it is inside my Class. I am not sure if the problem is that I don't build the connection to mysql inside my Class or not. Thanks for any helps!! 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: 

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

global $connection;

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

Greg Adamski
I see thank you.
Jerry
+1  A: 

It's interpreting $connection as a local variable in your check1() function.

Review the docs for variable scope. You probably have to define global $connection in your function.

Bill Karwin
Thanks for the reply.
Jerry