Okay, so. I have PDO
connection on MySQL
database on local server. This is the code for it
<?php
$servername = "localhost";
$username = "root";
$password = "";
try {
$dbh = new PDO('localhost:host=$servername;dbname=test', $username, $password);
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
When I open the page it gives me this error.
Error!: could not find a driver
It is obvious that drivers are missing but I have no idea how to install them. I already used
sudo pacman -S php
sudo pacman -S php-sqlite
sudo pacman -S mysql
Maybe I forgot some. Here is my /etc/php/php.ini file’s content
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
; If you wish to have an extension loaded automatically, use the following
; syntax:
;
; extension=modulename
;
; For example:
;
; extension=mysqli
;
; When the extension library to load is not located in the default extension
; directory, You may specify an absolute path to the library file:
;
extension=/path/to/extension/mysqli.so
;
; Note : The syntax used in previous PHP versions ('extension=<ext>.so' and
; 'extension='php_<ext>.dll') is supported for legacy reasons and may be
; deprecated in a future PHP major version. So, when it is possible, please
; move to the new ('extension=<ext>) syntax.
;
;extension=bcmath
;extension=bz2
;extension=calendar
extension=curl
;extension=dba
;extension=enchant
;extension=exif
;extension=ftp
extension=gd
;extension=gettext
;extension=gmp
;extension=iconv
;extension=imap
;extension=intl
;extension=sodium
;extension=ldap
;extension=mysqli
;extension=odbc
;zend_extension=opcache
;extension=pdo_dblib
extension=pdo_mysql
extension=bz2.so
extension=mcrypt.so
extension=mysqli
;extension=pdo_odbc
;extension=pdo_pgsql
extension=pdo_sqlite
;extension=pgsql
;extension=pspell
;extension=shmop
;extension=snmp
;extension=soap
;extension=sockets
extension=sqlite3
;extension=sysvmsg
;extension=sysvsem
;extension=sysvshm
;extension=tidy
;extension=xmlrpc
;extension=xsl
extension=zip
It didn’t let me upload the whole file so here is only part of it. If you need more details I will gladly add them in comments.
Maybe I am missing something obvious but I can’t find it for a few days.
EDIT
Sorry for not pointing this out.
I am using Manjaro Linux OS
3
Answers
I think PDO extensions are missing. Add the below extensions and restart your server
For windows server –
For Linux server –
Try this
When creating the connection, your DSN has
localhost
as the database type…from the manual
Also as you use single quotes, the servername will not be replaced, so use