2010-05-16 27 views
2

我想构建一个PHP类来检查MySql中的用户名和密码。 我得到“的mysql_query():提供的参数不是在d一个有效的MySQL-Link的资源:行38 \ 2010Portfolio \足球\ main.php”:”消息PHP类问题

当我移动。数据库有错误我的用户查询代码超出了我的类,它工作正常。只有当它在我的类内。 我不确定问题是我没有建立到我的班级内的MySQL连接或没有。谢谢任何帮助! ! 这里是我的代码

<?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(); 




     ?> 

我明白任何帮助!!!!

回答

3

问题是您的方法看不到连接对象。无论是使用在全球总体(

global $connection; 
在你的方法定义

(坏主意),或者尝试用一个单独的数据库连接会(代码示例都可以在网上找到)。

+0

我知道了,谢谢。 – FlyingCat 2010-05-16 04:29:22

1

它解释$connection作为在check1()函数的局部变量。

审查文档的variable scope。你可能在你的函数定义global $connection

+0

ŧ hanks的答复。 – FlyingCat 2010-05-16 04:28:41