1

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

<?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 语句上打印正确的值。现在,我将用户名存储在会话中并尝试打印时,这有效。代码有什么问题?

4

2 回答 2

1

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

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 }
于 2012-10-02T19:22:24.170 回答
1
  1. 您没有调用代码段中的代码。通话流程是怎样的?

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

于 2012-10-02T19:30:54.327 回答