我是新来这个,所以不要粗鲁:d问题与PDO连接
我有3个文件:database.php中,和的init.php user.php的
这里的init.php:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
require 'database.php';
require 'functions/user.php';
$errors = array();
这里database.php中:
<?php
$db_host = "localhost";
$db_name = "xxxx";
$db_user = "xxxx";
$db_pw = "xxxx";
try {
$conn = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_user, $db_pw);
} catch(PDOException $e) {
die("Verbindung fehlgeschlagen: " . $e->getMessage());
}
而这里user.php的:
<?php
function userExists($user) {
$sql = "SELECT * FROM user WHERE email = :email";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':email', $user);
$stmt->execute();
$results = $stmt->fetch(PDO::FETCH_ASSOC);
if(count($results) > 0) return true;
return false;
}
所以错误消息:
Notice: Undefined variable: conn in /mnt/web109/b2/35/57848035/htdocs/includes/functions/user.php on line 4 Fatal error: Call to a member function prepare() on null in /mnt/web109/b2/35/57848035/htdocs/includes/functions/user.php on line 4
功能userExists()被调用在另一个文件命名为login.php中。在login.php我已经需要init.php。当我想登录时出现错误消息。
所以我希望你能帮助我。
THX
开始与理解变量的作用域。 –
我认为这足以在ini.php中需要databse.php和user.php ..我几天前在视频中看到了它,它在那里起作用,所以我现在尝试它并获得错误。 – Reebal