PDO连接不起作用的外部文件

时间:2017-06-30 02:31:07

标签: php mysql pdo

我发现这个问题已经多次以某种形式提出,但是这个问题的其他版本的解决方案都不适用于我。

这两个文件没有问题:

/blog/login.php

<?php
include('core/init.php');

if (empty($_POST) === false){
    $username = $_POST['username'];
    $password = $_POST['password'];

    if (empty($username) || empty($password)) {
        $errors[] = 'Missing username and/or password.';
    } else if (user_exists($username) === false) {
        $errors[] = 'User doesn\'t exist.';
    }else if (user_active($username) === false) {
        $errors[] = 'User account not activated.';
    }else {
        //
    }

    print_r($errors);

}

?>

/blog/core/init.php

<?php
require('database/connect.php');
require('functions/users.php');
require('functions/general.php');

session_start();
$errors = array();
?>

我只是将它们包括在内,向您展示connect.php require() {间隔} users.php的{​​{1}}。{/ p>

/blog/core/database/connect.php

<?php
    $dbname = "xxx_forms";
    $servername = "mysql.xxx.com";
    $usr= "xxx_xxx";
    $pass = "xxxxxxxx";
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $usr, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

此连接本身不会触发任何错误......

/blog/core/functions/users.php

<?php

function user_exists($username) {
    $ret = '';
    try {
        $sql = "SELECT COUNT(id) FROM registration WHERE user =  '$username';";
        $q = $pdo->query($sql);
        $f = $q->fetch();
        $ret = $f[0];
    }   catch (PDOException $e) {
        die("Could not connect to the database $dbname :" . $e->getMessage());
    }
    return ($ret == 1) ? true : false;
}

然而,当我们到达users.php时,我总是在$q = $pdo->query($sql);行收到错误,显然是因为PHP不知道$pdo是什么。另一方面,当我包含来自connect.php的代码时,它不在外部文件中(完全如下,但未注释掉):

function user_exists($username) {
    //$dbname = "xxx_forms";
    //$servername = "mysql.xxx.com";
    //$usr= "xxx_xxx";
    //$pass = "xxxxxxxx";
    $ret = '';
    try {
        //$pdo = new PDO("mysql:host=$host;dbname=$dbname", $usr, $pass);
        $sql = "SELECT COUNT(id) FROM registration WHERE user =  '$username';";
        $q = $pdo->query($sql);
        $f = $q->fetch();
        $ret = $f[0];
    }   catch (PDOException $e) {
        die("Could not connect to the database $dbname :" . $e->getMessage());
    }
    return ($ret == 1) ? true : false;
}   

......一切都按照预期的方式运作。

在外部文件connect.php?

中完成PDO连接后如何使其工作

1 个答案:

答案 0 :(得分:0)

继续我的评论,你需要做这样的事情:

<强> /config.php

<?php
define('DS',DIRECTORY_SEPARATOR);
define('DB_HOST','localhost');
define('DB_NAME','database');
define('DB_USER','root');
define('DB_PASS','');
define('ROOT_DIR',__DIR__);
define('FUNCTIONS',ROOT_DIR.DS.'functions');
# Add the connection function
require_once(FUNCTIONS.DS.'connect.php');
# Start session
session_start();
# Get the connection
$pdo = connect();

<强> /functions/connect.php

function connect()
    {
        $pdo = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $pdo;
    }

<强> /functions/user_exists.php

<?php
function user_exists($pdo,$username)
    {
        $ret = 0;
        try {
            $sql = "SELECT COUNT(id) as count FROM registration WHERE user = :username";
            $q = $pdo->prepare($sql);
            $q->execute(array(":username"=>$username));
            $f   = $q->fetch(PDO::FETCH_ASSOC);
            $ret = $f['count'];
        }
        catch (PDOException $e) {
            die("Could not connect to the database ".DB_NAME.":" . $e->getMessage());
        }
        return ($ret == 1);
    }

以下是使用示例:

<强>的index.php

<?php
# Add the basic stuff
require(__DIR__.DIRECTORY_SEPARATOR.'config.php');
# Add our functions
require(FUNCTIONS.DS.'user_exists.php');
require(FUNCTIONS.DS.'general.php');
# Example of use
print_r(user_exists($pdo,'username@email.com'));