douyan8413 2015-05-07 20:16
浏览 83
已采纳

通过PHP连接到数据库时出错

I am trying to connect to my database via PHP but get blank page when I run through Safari(http://pushchat.local:11111/test/databasename.php)

I can see my datbasename.php file under http://pushchat.local:11111/test/ as shown below: enter image description here

Following is my PHP code:

try
{
    if (!defined('APPLICATION_ENV'))
        define('APPLICATION_ENV', getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : ‘development’);

    require_once '../api_config.php';
    $config = $config[APPLICATION_ENV];

    $pdo = new PDO(
        'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], 
        $config['db']['username'], 
        $config['db']['password'],
        array());

    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->query('SET NAMES utf8');

    echo 'Database connection successful!';
}
catch (Exception $e)
{
    echo 'Could not connect to the database. Reason: ' . $e;
}

I even tried to put a stop sigh using codebug but I think it never stops.

Thanks for your help.

  • 写回答

2条回答 默认 最新

  • duanchi1230 2015-05-07 20:46
    关注

    I'm Sorry But I'm Seeing database.php Not databasename.php.
    Have You Even Checked If The File Exists?
    Also Connecting To The Database Should Be Typed In Config File & You should require It In You're Main PHP File.
    For Example The Config File Should Look Like This:

    <?php
    $connection = mysql_connect('localhost', 'root', 'password');  //here is the host, username and password of mysql account.instead of localhost type in your websites domain name. 
    if (!$connection){                                              //and instead of password,type in your own password.
        die("Database Connection Failed" . mysql_error());
    }
    $select_db = mysql_select_db('test');
    if (!$select_db){
        die("Database Selection Failed" . mysql_error());
    }
    ?>
    

    & The Main PHP Should Look Like this:

    <?php
    require('config.php'); //requires the config.php page
    //rest of the code goes here...
    ?>
    

    Also On Line 10,You Have Missed A Closing Single-Quote.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器