PHP在准备SQL语句时抛出致命错误[重复]

This question already has an answer here:

In my PHP application, I want the user to be able to login with email, username and number recorded in the database. Yet, I

init.php

<?php
$server = 'localhost';
$username = 'default';
$password = 'xxxx';
$database = 'default';

try{
   $conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch(PDOException $e){
   die( "Connection failed: " . $e->getMessage());
}

login.php

<?php
session_start();
if(isset($_SESSION['user_name'])!='') {
header('Location: index.php');
}
include_once 'init.php';
//check if form is submitted
if (isset($_POST['login'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$records = $conn->prepare('SELECT username,email,number,password FROM users WHERE username = :login OR email = :login OR number = :login');
$records->bindParam(':login', $email);
$records->execute();
$results = $records->fetch(PDO::FETCH_ASSOC);

$message = '';

if(count($results) > 0 && password_verify($password, $results['password']) ){

$gome = $results['username'];$_SESSION['user_name'] = $gome;
$CookieExpire = 1000;
$time = 100 * 70 * 48 * 86400;
$time = time() + $time;
setcookie('username', '$gome', '$time');
setcookie('password', '$password', '$time');header("Location: /");

} else {
$message = 'Sorry, those credentials do not match';
}

}

?>

In returns i get this error: Fatal error: Call to a member function prepare() on a non-object in /var/www/u0377863/public_html/xx.com/dom/login.php on line 11:

    $records = $conn->prepare('SELECT username,email,number,password FROM users WHERE username = :login OR email = :login OR number = :login');
</div>

Try this code

<?php

session_start();

if(isset($_SESSION['user_name'])!='') {
    header('Location: index.php');
}

include_once 'init.php';

//check if form is submitted
if (isset($_POST['login'])) {
    $email = $_POST['email'];
    $password = $_POST['password'];
    $records = $conn->prepare("SELECT username,email,number,password FROM users WHERE ( username='$email' OR email = '$email' OR number = '$email')");
    $records->execute();
    $results = $records->fetch(PDO::FETCH_ASSOC);

    $message = '';

    if(count($results) > 0 && password_verify($password, $results['password']) ){

        $gome = $results['username'];$_SESSION['user_name'] = $gome;
        $CookieExpire = 1000;
        $time = 100 * 70 * 48 * 86400;
        $time = time() + $time;
        setcookie('username', '$gome', '$time');
        setcookie('password', '$password', '$time');header("Location: /");

    } else {
        $message = 'Sorry, those credentials do not match';
    }

}

?>

</div>

It is the scope of your variable.

try{
   $conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch(PDOException $e){
   die( "Connection failed: " . $e->getMessage());
}

should be

$conn = null;    
try{
   $conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch(PDOException $e){
   die( "Connection failed: " . $e->getMessage());
}

Your $conn is declared inside the try block, so it is not visible outside. By initializing it outside, it will be visible everywhere.

1) try to move your init.php code to login.php to make sure that it loads as expected.

2) Try to declare global variable $conn (global $conn;) before "new PDO.." to check if that is a scope issue.

3) If previous two steps do not shed the light on the problem please check if pdo is loaded (you may use functions listed at https://stackoverflow.com/a/6113469/1277044 to check it)