【问题标题】:PHP Retrieiving Column Data from databasePHP从数据库中检索列数据
【发布时间】:2012-10-02 18:44:44
【问题描述】:

我有一个较早的帖子here 但这些答案都没有奏效。所以这是我的整个课程代码:

<?php
session_start();
class Mysql {
    private $conn;

    function __construct() {
        $this->conn =  new PDO('mysql:host=***;dbname=***;charset=UTF-8','***','***') or 
                      die('There was a problem connecting to the database.');
    }

    function verify_Username_and_Pass($un, $pwd) {
        $query = "SELECT Username
                FROM Conference
                WHERE Username = :un AND Password = :pwd";

        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(':un', $un);
        $stmt->bindParam(':pwd', $pwd);
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            // User exist
            $stmt->bindColumn('First Name', $firstName); 
            $_SESSION["FirstName"] = $firstName; 
            die($_SESSION["FirstName"]);
            return true;
            $stmt->close();
        }
        else {
            // User doesn't exist
            //die("failure");
            return false;
            $stmt->close();
        }
    }
}
?>

我试过 fetch,我试过 bind_result 等等,但它们都没有在 die 语句上打印正确的值。现在,当我将用户名存储在会话中并尝试打印时,这 工作。代码有什么问题?

【问题讨论】:

  • 错误是什么?什么不起作用? die($_SESSION["FirstName"]); 是什么意思?
  • @JvdBerg 它实际上什么也没打印。但是如果我存储了用户名,那么它会打印正确的用户名。我什至用反引号尝试了 bindcolumn,但它仍然没有检索到正确的名字。

标签: php mysql database pdo


【解决方案1】:
  1. 您没有调用 sn-p 中的代码。调用流程是什么?

  2. 您不会仅检索Conference.First Name Conference.Username,因此您应该收到警告,除非您没有显示错误。你可能想要

"SELECT * FROM Conference...."

"SELECT FirstName From Conference WHERE Username = :un AND Password = :pwd";

  1. 可能是Conference.FirstName

  2. die(); 对于调试不是很有用。试试var_dump($_SESSION["FirstName"]); die();

【讨论】:

    【解决方案2】:

    我查看了您的代码,并在我自己的服务器上制作了一个工作版本。如果您有任何问题,请随时提出。

    class Mysql
    {
      private $conn;
      public  $error;
      public  $username;
    
      function __construct()
      {
        try {
          $this->conn = new PDO( 'mysql:host=localhost;dbname=****', 'root', '****' );
          $this->conn->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
    
        }
        catch ( Exception $e ) {
           $this->error = $e->getMessage();
        }
      }
    
      function verify_Username_and_Pass( $un, $pwd )
      {
        $query = "SELECT Username
                  FROM Conference
                  WHERE Username = :un AND Password = :pwd";
    
        $stmt = $this->conn->prepare( $query );
        if( !$stmt ) {
          $this->error = $this->conn->errorInfo();
          return false;        
        }
    
        $stmt->bindParam( ':un', $un );
        $stmt->bindParam( ':pwd', $pwd );
        $stmt->execute();
        if ( $stmt->rowCount() > 0 ) {
          // User exist
          $this->username = $stmt->fetchColumn();
          return true;
        } 
        else {
          // User doesn't exist
          return false;
        }
      }
    
    }
    
    session_start();
    
    $db = new Mysql();
    if( !$db->error ) {
      if( $db->verify_Username_and_Pass ( 'user', 'test' )) {
        $_SESSION["FirstName"] = $db->username;
      }
      else
        echo 'Unknown user';
    }
    
    var_dump( $db );
    

    脚本会输出这个:

    Unknown user
    
    object(Mysql)#1 (3) { 
    ["conn":"Mysql":private]=> object(PDO)#2 (0) { } 
    ["error"]=> array(3) 
       { [0]=> string(5) "42S02" 
         [1]=> int(1146) 
         [2]=> string(39) "Table   'xxxx.Conference' doesn't exist" } 
    ["username"]=> NULL }
    

    【讨论】:

    • 看到你在调用一个你从未设置过的变量$db-&gt;firstName 这真的有效吗??
    • @PhilipWhitehouse:感谢您指出这一点。我没有对现有用户进行测试。更正并更新了代码。
    • 不用担心,根据他的代码编写了我自己的修复列表。希望有足够的继续:-)
    • @JvdBerg 拳头名称与用户名不同,如果是,我不会发布问题,因为我已经有了他们的用户名(这是他们输入的)
    • @CharlieYabben:但即使在这种情况下,您也会遇到错误(除非您禁用了错误报告)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2019-11-19
    • 2016-03-29
    相关资源
    最近更新 更多