duanpiao6679 2013-11-12 04:14
浏览 16
已采纳

被PDO中的致命错误所困扰? [关闭]

I'm receiving a Fatal error output to my webpage. This is the error:


Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host '$host' (20)' in /Users/aaronwilson/Desktop/testing_server/ATOM_CMS/config/setup.php:14 Stack trace: #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} thrown in /Users/aaronwilson/Desktop/testing_server/ATOM_CMS/config/setup.php on line 14


Here is the code that is triggering the error on my Setup.php page:

<?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);
?>

Any suggestions would be appreciated.. :)

  • 写回答

1条回答 默认 最新

  • dongzhong2018 2013-11-12 04:18
    关注

    In this line:

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

    You have used single quotes. PHP doesn't expand variables in strings where single quotes have been used, so PDO sees the hostname as $host.

    Try

    $dbc = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
                   ^                               ^ // note double quotes.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误