PDOException on Windows Server


PDOException on Windows Server

我对MySQL和PHP还很陌生,所以请耐心等待。经过一番研究,我发现。。。让我非常难过。。。显然,我的网站托管在GoDaddy上的Windows服务器上(默认情况下),该服务器不支持PDO。

我最近刚刚通过我发布的另一个问题的推荐,将所有数据库调用切换为使用准备好的语句。我现在发现这些没有运行,我得到了一个严重的错误:

Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in D:'Hosting'8446415'html'ROOTFOLDER'php'Connect.php:8
Stack trace: #0 D:'Hosting'8446415'html'ROOTFOLDER'php'Connect.php(8): PDO->__construct('mysql:host=HOSTNAME', 'DBNAME', 'PASSWORD') 
#1 D:'Hosting'8446415'html'ROOTFOLDER'admin.php(67): Connect->connect()
#2 {main} thrown in D:'Hosting'8446415'html'ROOTFOLDER'php'Connect.php on line 8

这是Connect.php:

<?php
    class Connect {
        const expAddress = "HOSTNAME";
        const expUser = "USERNAME";
        const expPwd = "PASSWORD";
        function connect() {
            $db = new PDO('mysql:host='.self::expAddress.';dbname='.self::expUser.';charset=UTF-8', self::expUser, self::expPwd);
            if(!$db) {
                die("<p>Could not establish a connection to the database.</p>");
                include('footer.php');  
            }
            else {
                return $db; 
            }
        }
    }
?>

那么,这里的解决方案是什么呢?我不知道支持什么,有人告诉我要回避所有的mysql_*语句。

我无法将我的托管帐户升级到Linux,尽管这是最简单的我需要使用mysqli_*语句吗?这个特定的调用将如何更改,以便输入不会被注入?

$stmt = $db->prepare("SELECT * FROM logins WHERE username=? AND password=?");
$stmt->execute(array($user, $pass));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

您需要确保安装了pdo_mysql。检查phpinfo()是否为pdo_mysql。。。如果您知道它已经安装,您可能只需要在php.ini…中取消注释extension=php_pdo_mysql.dll

为了捕捉你的创建错误并很好地显示它,你可以这样修改你的代码:

try{
    $db = new PDO('mysql:host='.self::expAddress.';dbname='.self::expUser.';charset=UTF-8', self::expUser, self::expPwd);
} catch (PDOException $e) {
    die('Connect Failed: ' . $e->getMessage());
}
return $db;

如果您确实可以访问php.ini,请确保取消注释此行:

extension_dir = "ext"