douyin4561 2014-03-19 10:14
浏览 56
已采纳

无法通过PHP PDO类连接到数据库

I'm trying to connect to my mySQL database using the PDO class in PHP.

Here is my Code :

// Connects to Our Database via PDO.
if($local) {
    try {
        $db = new PDO("mysql:=localhost;dbname=bbc_archive;port=3306", "root", "");
        $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        $db->exec("SET NAMES 'utf8'");
        } catch(Exception $e) {
            echo "Connection to the DataBase was not possible. ";
            die();
        }
    } else {
    try {
        $db = new PDO("mysql:=bbcarchive.db.11505263.hostedresource.com;dbname=bbcarchive", "bbcarchive", "myPassword");
        $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        $db->exec("SET NAMES 'utf8'");
    } catch(Exception $e) {
        echo "Connection to the live DataBase was not possible. ";
        die();
    }
}

The $local variable is defined before and determines whether or not the script is running on a the live server or a test server.

When running in my local environment everthing works fine but on my live server it echo's out "Connection to the live DataBase was not possible." from the catch block.

I've contacted my host provider (godaddy) and they think it's a coding error. I've also, obviously, checked the hostname, dbname, username and password a 100 times and it's all correct. I just can't see the problem!

How can i do this ?

  • 写回答

1条回答 默认 最新

  • duancai9010 2014-03-19 10:18
    关注

    Your DSN seems to be incorrect. The documentation on MySQL DSNs indicates that it should look somewhat like this:

    $db = new PDO("mysql:host=localhost;dbname=bbc_archive;port=3306", "root", "");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里