PDO没有选择数据库

时间:2014-01-23 22:18:35

标签: php mysql login pdo database-connection

我为网站创建了一个登录系统但是收到以下错误消息:

  

警告:PDOStatement :: execute():SQLSTATE [3D000]:无效的目录名称:1046尝试登录时,第18行的C:\ wamp \ www \ Bitev3 \ core \ classes \ users.php中没有选择数据库用户。

登录php文件

<?php
require 'core/init.php';
$general->logged_in_protect();
if (empty($_POST) === false) {

    $email = trim($_POST['email']);
    $password = trim($_POST['password']);

    if (empty($email) === true || empty($password) === true) {
        $errors[] = 'Sorry, but we need your email and password.';
    } else if ($users->email_exists($email) === false) {
        $errors[] = 'Sorry that email doesn\'t exists.';
    } else if ($users->email_confirmed($email) === false) {
        $errors[] = 'Sorry, but you need to activate your account. 
                     Please check your email.';
    } else {

        $login = $users->login($email, $password);
        if ($login === false) {
            $errors[] = 'Sorry, that email/password is invalid';
        }else {
            // email/password is correct and the login method of the $users object returns the user's id, which is stored in $login.

            $_SESSION['UserID'] =  $login; // The user's id is now set into the user's session  in the form of $_SESSION['id'] 

            #Redirect the user to home.php.
            header('Location: home.php');
            exit();
        }
    }
} 
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="css/style.css" >
    <title>Login</title>
</head>
<body>  
    <div id="container">
        <ul>
            <li><a href="index.php">Home</a></li>
            <li><a href="register.php">Register</a></li>
            <li><a href="login.php">Login</a></li>
        </ul>
        <h1>Login</h1>

        <?php if(empty($errors) === false){

            echo '<p>' . implode('</p><p>', $errors) . '</p>';          

        } 
        ?>
        <form method="post" action="">
            <h4>Email:</h4>
            <input type="text" name="email">
            <h4>Password:</h4>
            <input type="password" name="password">
            <br>
            <input type="submit" name="submit">
        </form>
    </div>
</body>
</html>

init php file

<?php 
#starting the users session
session_start();
require 'connect/database.php';
require 'classes/users.php';
require 'classes/general.php';

$users      = new Users($db);
$general    = new General();

$errors     = array();

数据库php文件

<?php
$config = array(
    'host' => 'locathost',
    'username' => 'root',
    'password' => 'pass123',
    'dbname' => 'bitev2'
);

$db =new PDO ('mysql:host'. $config['host'] . ':dbname=' . $config['dbname'], $config['username'], $config['password']);

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

一般的php文件

class General{


#Check if the user is logged in.
    public function logged_in () {
        return(isset($_SESSION['UserID'])) ? true : false;
    }

    #if logged in then redirect to home.php
    public function logged_in_protect() {
        if ($this->logged_in() === true) {
            header('Location: home.php');
            exit();     
        }
    }

    #if not logged in then redirect to index.php
    public function logged_out_protect() {
        if ($this->logged_in() === false) {
            header('Location: index.php');
            exit();
        }   
    }}

用户php文件

<?php 
class Users{

    private $db;

    public function __construct($database) {
        $this->db = $database;
    }


public function email_exists($email) {

    $query = $this->db->prepare("SELECT COUNT(`UserID`) FROM `users` WHERE `Email`= ?");
    $query->bindValue(1, $email);

    try{

        $query->execute();
        $rows = $query->fetchColumn();

        if($rows == 1){
            return true;
        }else{
            return false;
        }

    } catch (PDOException $e){
        die($e->getMessage());
    }

}

public function register($firstname, $lastname, $gender, $password, $email){

    $time       = time();
    $ip         = $_SERVER['REMOTE_ADDR'];
    $email_code = sha1($firstname + $lastname + microtime());
    $password   = sha1($password);

    $query  = $this->db->prepare("INSERT INTO `users` (`FirstName`, `LastName`, `Email`, `Password`, `Gender`, `ip`, `time`, `Email_code`) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ");

    $query->bindValue(1, $firstname);
        $query->bindValue(2, $lastname);
    $query->bindValue(3, $email);
    $query->bindValue(4, $password);
    $query->bindValue(5, $gender);
    $query->bindValue(6, $ip);
        $query->bindValue(7, $time);
    $query->bindValue(8, $email_code);

    try{
        $query->execute();


    }catch(PDOException $e){
        die($e->getMessage());
    }   
}
public function activate($email, $email_code) {

        $query = $this->db->prepare("SELECT COUNT(`UserID`) FROM `users` WHERE `Email` = ? AND `Email_code` = ? AND `confirmed` = ?");

        $query->bindValue(1, $email);
        $query->bindValue(2, $email_code);
        $query->bindValue(3, 0);

        try{

            $query->execute();
            $rows = $query->fetchColumn();

            if($rows == 1){

                $query_2 = $this->db->prepare("UPDATE `users` SET `confirmed` = ? WHERE `Email` = ?");

                $query_2->bindValue(1, 1);
                $query_2->bindValue(2, $email);             

                $query_2->execute();
                return true;

            }else{
                return false;
            }

        } catch(PDOException $e){
            die($e->getMessage());
        }
    }

        public function login($email, $password) {

    $query = $this->db->prepare("SELECT `Password`, `UserID` FROM `users` WHERE `Email` = ?");
    $query->bindValue(1, $email);

    try{

        $query->execute();
        $data               = $query->fetch();
        $stored_password    = $data['Password'];
        $id                 = $data['UserID'];


        if($stored_password === sha1($password)){
            return $id; 
        }else{
            return false;   
        }

    }catch(PDOException $e){
        die($e->getMessage());
    }
}

public function email_confirmed($email) {

    $query = $this->db->prepare("SELECT COUNT(`UserID`) FROM `users` WHERE `Email`= ? AND `confirmed` = ?");
    $query->bindValue(1, $email);
    $query->bindValue(2, 1);

    try{

        $query->execute();
        $rows = $query->fetchColumn();

        if($rows == 1){
            return true;
        }else{
            return false;
        }

    } catch(PDOException $e){
        die($e->getMessage());
    }

}
public function userdata($id) {

    $query = $this->db->prepare("SELECT * FROM `users` WHERE `UserID`= ?");
    $query->bindValue(1, $id);

    try{

        $query->execute();

        return $query->fetch();

    } catch(PDOException $e){

        die($e->getMessage());
    }
}
public function get_users() {


    $query = $this->db->prepare("SELECT * FROM `users` ORDER BY `time` DESC");

    try{
        $query->execute();
    }catch(PDOException $e){
        die($e->getMessage());
    }


    return $query->fetchAll();
}
}

4 个答案:

答案 0 :(得分:3)

错字?

$db = new PDO ('mysql:host'. $config['host'] . ':dbname=' . $config['dbname'], $config['username'], $config['password']);

应该是

$db = new PDO ('mysql:host='. $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);

答案 1 :(得分:3)

你有一个错字,可能会导致一些问题:

    $config = array(
    'host' => 'locathost',
    'username' => 'root',
    'password' => 'pass123',
    'dbname' => 'bitev2'
   );

尝试将locathost更改为localhost。

答案 2 :(得分:1)

$db =new PDO ('mysql:host'. $config['host'] . ':dbname=' . $config['dbname'], $config['username'], $config['password']);

这需要:

$db =new PDO ('mysql:host='. $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);

答案 3 :(得分:-2)

$db =new PDO ('mysql:host='. $config['host'] . ';:dbname=' . $config['dbname'], $config['username'], $config['password']);