在我的PHP应用程序中,我希望用户能够使用数据库中记录的电子邮件,用户名和号码登录。然而,我
的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';
}
}
?>
在返回时我收到此错误:
致命错误:调用成员函数prepare()
第11行/var/www/u0377863/public_html/xx.com/dom/login.php
中的非对象:
$records = $conn->prepare('SELECT username,email,number,password FROM users WHERE username = :login OR email = :login OR number = :login');
答案 0 :(得分:-2)
试试此代码
mylogit<- multinom(to ~ RealAge, mydata)
&#13;
答案 1 :(得分:-2)
1)尝试将 init.php 代码移至login.php,以确保其按预期加载。
2)尝试在&#34;新PDO之前声明全局变量$ conn(global $ conn;)。&#34;检查这是否是范围问题。
3)如果前两个步骤没有解决问题,请检查是否加载了 pdo (您可以使用https://stackoverflow.com/a/6113469/1277044中列出的功能进行检查)
答案 2 :(得分:-3)
这是变量的范围。
try{
$conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch(PDOException $e){
die( "Connection failed: " . $e->getMessage());
}
应该是
$conn = null;
try{
$conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
} catch(PDOException $e){
die( "Connection failed: " . $e->getMessage());
}
您的$ conn在try块中声明,因此在外部不可见。通过在外面初始化它,它随处可见。