我是一個新手PHP程序員,直到幾天前才聽說過PDO。到目前爲止,我只是在PHP中爲我的網站使用了默認的MySQL函數,但從我一直在閱讀的內容來看,似乎我應該切換到PDO。如何讓PDO啓動並運行?
我重新編譯了Apache,使PDO,但是當我嘗試用下面的代碼連接:
try
{
$dbh = new PDO('mysql:host=localhost;dbname=' . $dbname, $user, $pass);
}
catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
我得到的錯誤:
Error!: could not find driver
我跑的phpinfo(),發現如下有關PDO:
'./configure' '--disable-fileinfo' '--disable-phar' '--enable-bcmath' '--enable-calendar' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-mbstring' '--enable-pdo=shared' '--enable-sockets' '--prefix=/usr/local' '--with-apxs2=/usr/local/apache/bin/apxs' '--with-curl=/opt/curlssl/' '--with-freetype-dir=/usr' '--with-gd' '--with-gettext' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/usr' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libxml-dir=/opt/xml2/' '--with-mcrypt=/opt/libmcrypt/' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-mysqli=/usr/bin/mysql_config' '--with-openssl=/usr' '--with-openssl-dir=/usr' '--with-pcre-regex=/opt/pcre' '--with-pdo-sqlite=shared' '--with-png-dir=/usr' '--with-sqlite=shared' '--with-xpm-dir=/usr' '--with-zlib' '--with-zlib-dir=/usr'
PDO
PDO support enabled
PDO drivers sqlite, sqlite2
pdo_sqlite
PDO Driver for SQLite 3.x enabled
SQLite Library 3.7.7.1
如何獲得PDO的工作?
感謝您的幫助!
您是否在php.ini中啓用了PDO擴展? – Tio
一些提示:'PDO驅動程序sqlite,sqlite2','新的PDO('mysql'...'和'錯誤!:找不到驅動程序' – Esailija
我會閱讀,因爲你只有sqlite和PDO的sqlite2驅動程序,因此它的抱怨不是它不能做PDO,但它不能做mysql – BugFinder