【问题标题】:PHP/MYSQL Retrieve Name based on another criterionPHP/MYSQL 根据另一个标准检索名称
【发布时间】:2012-09-23 14:15:34
【问题描述】:

所以我有一个登录系统,我想检索登录人的名字。这是我的 php:

function verify_Username_and_Pass($un, $pwd) {
        $query = "SELECT `First Name`, Username, Password
                FROM table
                WHERE Username = :un AND Password = :pwd
                LIMIT 1";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':un', $un);
        $stmt->bindParam(':pwd', $pwd);
        $stmt->execute();

        if ($stmt->rowCount() > 0) {
            // User exist
            return true;
            $stmt->close();
        }
        else {
            // User doesn't exist
            return false;
            $stmt->close();
        }
    }

这是具有 1 个私有变量 $conn 的类的一部分。登录工作完美,但我只想得到这个人的名字。我该怎么做?

【问题讨论】:

标签: php mysql database login pdo


【解决方案1】:

只是改变查询语句?

    $query = "SELECT `First Name`
            FROM table 
            WHERE Username = :un AND Password = :pwd 
            LIMIT 1"; 

如果这引发错误,您将不得不展示更多该类正在执行的操作来管理数据库事务

【讨论】:

    【解决方案2】:

    只需更改这一行,在查询中只选择First Name

     $query = "SELECT `First Name`, Username, Password
                FROM table
                WHERE Username = :un AND Password = :pwd
                LIMIT 1";` 
         to 
    
     $query = "SELECT `First Name`
                FROM table
                WHERE Username = :un AND Password = :pwd
                LIMIT 1";`
    

    【讨论】:

      【解决方案3】:

      您需要将结果绑定如下

          if ($stmt->rowCount() > 0) {
              $stmt->bind_result($fname, $uname, $pwd);
              $stmt->fetch()
              echo $fname // here you get firsname
      
              // either you can return this $fname or store into session variable for further
      
              // User exist
              return true;
              $stmt->close();
          }
          else {
              // User doesn't exist
              return false;
              $stmt->close();
          }
      

      【讨论】:

        【解决方案4】:

        在您返回 true 的部分中,您可以改为返回实际的用户数据(并且带有数据的数组无论如何都会评估为 true)。

        警告,您应该使用散列密码。不要存储密码 y 普通的。

        【讨论】:

          【解决方案5】:

          首先,永远不要从数据库中获取密码,这是非常糟糕的做法。

          其次,如果只返回一行,您只想接受用户是正确的。

          最后bindColumn 是您要找的。​​p>

          <?php
          function verify_Username_and_Pass($un, $pwd) {
              $query = "SELECT `First Name`, Username
                        FROM table
                        WHERE Username = :un AND Password = :pwd";
              // Don't limit the query to only one, if there is a chance that you can
              // return multiple rows, either your code is incorrect, bad data in the database, etc...
          
              $stmt = $this->conn->prepare($query);
              $stmt->bindParam(':un', $un);
              $stmt->bindParam(':pwd', $pwd);
              $stmt->execute();
          
              // Only assume proper information if you ONLY return 1 row.
              // Something is wrong if you return more than one row...
              if ($stmt->rowCount() == 1) {
                  // User exist
                  $stmt->bindColumn('First Name', $firstName);
                  $stmt->bindColumn('Username', $username);
                  // You can now refer to the firstName and username variables.
                  return true;
                  $stmt->close();
              } else {
                  // User doesn't exist
                  return false;
                  $stmt->close();
              }
          }
          ?>
          

          这应该适合你。

          【讨论】:

          • 啊,好的,谢谢。我应该怎么做才能验证登录?
          • 好吧,你会想以某种容量散列你的密码。最好不要 MD5,那被认为是过时的,它运行得太快了。这完全是一个不同的问题。只要确保你从不向数据库询问密码,你总是可以告诉数据库密码可能是什么,但如果你抓住它,人们就有机会阅读它。这是一个非常糟糕的主意......
          • 啊好吧,完成这件事后,我将着手实施 bcrypt。另外,我尝试将您的代码放入 if 语句中的$stmt-&gt;bindColumn('First Name', $firstName);,但之后我尝试了die($firstName);,但没有打印任何内容。怎么了?
          • 您实际上是在回显 $firstName 变量吗?它不会只是打印自己。
          • 嗯,我没试过。但我的最终目标是把它放在一个会话中,所以我做了这样的事情:$stmt-&gt;bindColumn('First Name', $firstName); $_SESSION["FirstName"] = $firstName; die($_SESSION["FirstName"]); 这适用于用户名,但不适用于名字。
          猜你喜欢
          • 2019-06-11
          • 2019-06-12
          • 2021-02-24
          • 1970-01-01
          • 2021-12-26
          • 1970-01-01
          • 2012-07-05
          • 2012-09-01
          • 1970-01-01
          相关资源
          最近更新 更多