开发者

PHP Class Question

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 connect开发者_运维技巧ion 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!!!!


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).


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜