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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP在准备SQL语句时抛出致命错误[重复]相关的知识,希望对你有一定的参考价值。

这个问题在这里已有答案:

在我的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';
}

}

?>

在返回中我得到此错误:致命错误:在第11行的prepare()中的非对象上调用成员函数/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');
答案

试试这个代码

<?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';
	}

}

?>
另一答案

1)尝试将您的init.php代码移动到login.php,以确保它按预期加载。

2)尝试在“new PDO ..”之前声明全局变量$ conn(global $ conn;),以检查这是否是范围问题。

3)如果前两个步骤没有解决问题,请检查是否已加载pdo(您可以使用https://stackoverflow.com/a/6113469/1277044中列出的功能进行检查)

另一答案

它是变量的范围。

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块中声明,因此在外部不可见。通过在外面初始化它,它随处可见。

以上是关于PHP在准备SQL语句时抛出致命错误[重复]的主要内容,如果未能解决你的问题,请参考以下文章

为啥mysql PDO不会在失败时抛出错误[重复]

PHP:包含和需要文件时抛出错误

PHP:包含和需要文件时抛出错误

php mysqli在嵌套循环中准备语句抛出命令或同步错误[重复]

在 oracle apex 中使用 sqlerm 和异常时抛出重复的错误代码

使用 mysqli_stmt 时 PhP 可捕获的致命错误