被PDO中的致命错误所困扰?

时间:2013-11-12 04:14:56

标签: php html mysql pdo content-management-system

我收到致命错误输出到我的网页。这是错误:


致命错误:未捕获的异常'PDOException',消息'SQLSTATE [HY000] [2005] /Users/aaronwilson/Desktop/testing_server/ATOM_CMS/config/setup.php中的未知MySQL服务器主机'$ host'(20)' :14堆栈跟踪:#0 /Users/aaronwilson/Desktop/testing_server/ATOM_CMS/config/setup.php(14):PDO-> __ construct('mysql:host = $ hos ...','atomcmsadmin',' ** ')#1 /Users/aaronwilson/Desktop/testing_server/ATOM_CMS/config/setup.php(22):dbConnect('atomcmsadmin',' ** ' ,'localhost','Atom_CMS')#2 /Users/aaronwilson/Desktop/testing_server/ATOM_CMS/index.php(2):include('/ users / aaronwil ...')#3 {main}抛入/用户第14行/aaronwilson/Desktop/testing_server/ATOM_CMS/config/setup.php


以下是在我的Setup.php页面上触发错误的代码:

<?php ## Setup Document

// host(or location of the database), username, password, database name
//Variables for connecting to your database.
//These variable values come from your hosting account.

error_reporting(E_ALL);
ini_set('display_errors', 1);
require('functions/sandbox.php');

$host = "localhost";
$user = "atomcmsadmin";
$pass = "*******";
$dbname = "Atom_CMS";

//Connecting to your database

function dbConnect($user, $pass, $host, $dbname) {
$dbc = new PDO('mysql:host=$host;dbname=$dbname', $user, $pass);

try {
    global $dbc;
    }
catch (PDOException $e) {
    echo $e->getMessage();
}
}
if (dbConnect($user, $pass, $host, $dbname)) {
print('worked');
}

if ($_GET ['page'] == '') {
$pg = 'home';} 
else {
$pg = $_GET ['page']; }
$page_title = get_title($dbc, $pg);
?>

任何建议都将受到赞赏.. :)

1 个答案:

答案 0 :(得分:1)

在这一行:

$dbc = new PDO('mysql:host=$host;dbname=$dbname', $user, $pass);

您使用过单引号。 PHP不会在使用单引号的字符串中扩展变量,因此PDO将主机名视为$host

尝试

$dbc = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
               ^                               ^ // note double quotes.