thinkphp 解决 “Undefined class constant ‘MYSQL_ATTR_INIT_COMMAND’“的办法

作者

如果遇到了 Undefined class constant 'MYSQL_ATTR_INIT_COMMAND,就应该考虑php是否安装了和运行了"pdo-mysql"。pdo - php data ojbectphp数据对象。

thinkphp没有安装pdo-mysql
thinkphp没有安装pdo-mysql

可以在phpinfo中查看是否加载了 pdo_mysql,如果加载了,应该就不会有这个问题。
pdo-mysql
pdo-mysql

linux环境下从源代码安装的话,默认是会安装pdo(--disable-pdo 可以禁止) , 但是 php-mysql(--with-pdo-mysql )默认,没有编译。所以编译的时候需要 --with-pdo-mysql

./configure -h | grep pdo
  --disable-pdo           Disable PHP Data Objects support
  --with-pdo-dblib=DIR    PDO: DBLIB-DB support.  DIR is the FreeTDS home directory
  --with-pdo-firebird=DIR PDO: Firebird support.  DIR is the Firebird base
  --with-pdo-mysql=DIR    PDO: MySQL support. DIR is the MySQL base directory
  --with-pdo-oci=DIR      PDO: Oracle OCI support. DIR defaults to \$ORACLE_HOME.
                          Use --with-pdo-oci=instantclient,prefix,version
                            --with-pdo-oci=instantclient,/usr,11.2
                            --with-pdo-oci=instantclient,/usr,10.2.0.4
  --with-pdo-odbc=flavour,dir
                            --with-pdo-odbc=unixODBC
                            --with-pdo-odbc=generic,dir,libname,ldflags,cflags
                          When built as 'shared' the extension filename is always pdo_odbc.so
  --with-pdo-pgsql=DIR    PDO: PostgreSQL support.  DIR is the PostgreSQL base
  --without-pdo-sqlite=DIR

如果是windows环境查看,直接加载这两个dll,然后重启。

extension=php_pdo.dll
extension=php_pdo_mysql.dll

--with-pdo-mysql

PDO::MYSQL_ATTR_INIT_COMMAND (integer)
Command to execute when connecting to the MySQL server. Will automatically be re-executed when reconnecting.
Note, this constant can only be used in the driver_options array when constructing a new database handle.

回复

电子邮件地址不会被公开。