doujiexi1824 2010-05-16 04:02
浏览 26
已采纳

PHP类问题

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条回答 默认 最新

  • drfu29983 2010-05-16 04:19
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line