dongshi1148 2014-04-14 04:06
浏览 116
已采纳

未定义类常量中未定义的类常量“MYSQL_ATTR_INIT_COMMAND”(PHP 5.5.3)

I have a config file that connects to a remote database, however i keep receiving an error that i don't know how to fix. In the earlier versions it was meant to be a bug but i assumed it would have been fixed by 5.5.3.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');

$host = "localhost";
$dbname = "registration";
$username = "databaseEditor";
$password = "yolo10";

// 1002 = MYSQL_ATTR_INIT_COMMAND
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');

try
{
    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username$
}
catch(PDOException $ex)
{
    die("Failed to connect to the database: " . $ex->getMessage());
}

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

header('Content-Type: text/html; charset=utf-8');

echo "File works";
?>

I keep receiving the error "Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'". I have tried to use 1002 instead of 'MYSQL_ATTR_INIT_COMMAND' but then i end up with the error "syntax error, unexpected '1002' (T_LNUMBER)" instead and i have also have installed the newest php-mysql extension.

展开全部

  • 写回答

3条回答 默认 最新

  • douba9020 2014-04-14 04:15
    关注

    Try editing your php.ini:

    On a windows server you can add the following lines in your php.ini

     extension=php_pdo.dll
     extension=php_pdo_mysql.dll
    

    On a Linux server you can compile php with the following option --with-pdo-mysql

    In your php.ini, add the following lines

     extension=pdo.so
     extension=pdo_mysql.so
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部