有一点pdo问题,我现在已经工作了一段时间。由于我不知道这里有什么问题,我想把它带到这个清单。也许有些人知道更多...
我有一个登录网站,可以根据mysql驱动的数据库检查用户和密码。当在同一个文件中建立pdo连接一切正常时,可以登录,没有任何问题。正如它应该工作......
但是,当我将数据库连接部分移动到我从另一个文件中包含的单独函数时,pdo就失败了,并且给了我:
SQLSTATE [28000] [1045]拒绝访问用户'...'@'...'(使用 密码:否) 致命错误:在第41行的/ ... / ... / ...中的非对象上调用成员函数prepare()
为了清楚起见,这里是代码:
版本1:
这有效:
<?php
require "./vars_and_functions.php";
/* open database connection */
try {
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
/* query */
$query = "SELECT uname, passw FROM members WHERE uname = ? AND passw = ?";
$q = $pdo->prepare($query);
$q->execute(array($u_name, $p_word_md5));
$result = $q->rowCount();
if($result == 1) { /* we have a match */
/* close the database connection */
$pdo = null;
/* and redirect */
header("...");
} /* if */
else { /* wrong credentials */
/* close the database connection */
$pdo = null;
/* and go back to the login page */
header("...");
} /* else */
} /* try */
catch(PDOException $e) {
echo $e->getMessage();
} /* catch */
?>
这是版本2
这不起作用:
<?php
require "./vars_and_functions.php";
/* open database connection */
$pdo = database_connection();
/* query */
$query = "SELECT uname, passw FROM members WHERE uname = ? AND passw = ?";
$q = $pdo->prepare($query);
$q->execute(array($u_name, $p_word_md5));
$result = $q->rowCount();
if($result == 1) { /* we have a match */
/* close the database connection */
$pdo = null;
/* and redirect */
header("...");
} /* if */
else { /* wrong credentials */
/* close the database connection */
$pdo = null;
/* and go back to the login page */
header("...");
} /* else */
} /* try */
catch(PDOException $e) {
echo $e->getMessage();
} /* catch */
?>
我的includefile vars_and_functions.php如下所示:
$db_host = "...";
$db_name = "...";
$db_user = "...";
$db_pass = "...";
function database_connection() {
try {
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
}
catch(PDOException $e) {
echo $e->getMessage();
}
return $pdo;
}
我唯一真正的不同之处在于,在这里,pdo连接是通过函数调用完成的,而函数则位于包含文件vars_and_functions.php中。
这里有什么问题?
答案 0 :(得分:3)
您的函数database_connection()
没有在正确的范围内接收连接变量,因此在尝试连接时它们不会被设置,因此传递为NULL
,并且PDO默认连接主机到localhost
。
将它们作为参数传递给函数:
// Defined at global scope...
$db_host = "...";
$db_name = "...";
$db_user = "...";
$db_pass = "...";
// Pass the 4 variables as parameters to the function, since they were defined at global
// scope.
function database_connection($db_host, $db_name, $db_user, $db_pass) {
try {
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
}
// Called as:
$pdo = database_connection($db_host, $db_name, $db_user, $db_pass);
如果您仅在连接函数中使用这些变量并且在其他地方不需要它们,请考虑在函数范围内定义它们,这样可以节省您将它们作为参数传递。
function database_connection() {
// Only needed here, so define in function scope
$db_host = "...";
$db_name = "...";
$db_user = "...";
$db_pass = "...";
try {
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
}
最终且通常最不可取的选择是在您完成时在全局范围内定义变量,但在函数中通过$GLOBALS[]
(或global
关键字)访问它们:
function database_connection() {
try {
$pdo = new PDO("mysql:host={$GLOBALS['db_host']};dbname={$GLOBALS['db_name']}", $GLOBALS['db_user'], $GLOBALS['db_pass']);
}
请注意,如果您正在开发error_reporting
并且display_errors
,那么您会看到有关未定义变量的注意事项。
error_reporting(E_ALL);
ini_set('display_errors', 1);
答案 1 :(得分:0)
除了Michael Berkowski的回答,您还可以传递全局关键字:
function database_connection() {
global $db_host, $db_name, etc;
// your code here
}
有关PHP中变量范围的更多信息,请参阅http://php.net/manual/en/language.variables.scope.php。