PHP 类问题

发布于 2024-09-01 08:39:52 字数 1400 浏览 6 评论 0原文

我正在尝试构建一个 PHP 类来检查 MySql 中的用户名和密码。 我收到“mysql_query():提供的参数不是 D:\2010Portfolio\football\main.php 第 38 行中的有效 MySQL-Link 资源”数据库有错误:“ 消息。

当我移动时我的 userQuery 代码在我的类之外,仅当它在我的类内时才正常。 我不确定问题是否是我没有在我的类中建立与 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();




      ?>

,感谢您的帮助!!!!

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!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城凉梦凉人心 2024-09-08 08:39:53

问题是您的方法无法看到连接对象。要么在方法定义中使用全局 (

global $connection;

(一般来说是个坏主意),要么尝试使用单例数据库连接(代码示例可以在整个网络上找到)。

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

思念绕指尖 2024-09-08 08:39:53

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

查看变量范围的文档。您可能必须在函数中定义 global $connection

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文