Could not connect to the database mytable: could not find driver - php

I had the php version 5 but now i upgraded to php 7 and i am getting problems that a didn't had with the older version.
One that i'm still trying to solve is this:
Could not connect to the database mydatabase :could not find driver (this appear when i run my script).
I tryed to open phpmyadmin and also appeared an error:
The mysqli extension is missing. Please check your PHP configuration.
After a lot of search i still can find a solution, is this related to the version of my mysql on xampp? It need to be upgraded too?
Apache version: 2.4
mysqlnd 5.0.12
********************** EDIT*****************
Here is the code that makes the connection to database:
function connection()
{
$host = 'localhost';
$dbname = 'mydatabase';
$username = 'root';
$password = '';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname;", $username, $password);
$conn->exec("SET CHARACTER SET utf8");
// echo "Connected to $dbname at $host successfully.";
}
catch(PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
echo "Line: " . __LINE__;
}
return $conn;
} //connection

Either install the mysqli driver on your server or use PDO, if that's available. Use this code to find out more information about the installed drivers:
<?php phpinfo() ?>

Go to your php.ini file and uncomment this line
extension=mysqli
Then restart your local server

This was how i solved the problem:
1º rename php.ini-developer to php.ini
2º add extensions:
extension_dir = "D:\Programs\xampp\php\ext"
extension=php_mysqli.dll
extension=pdo_mysql
extension=mbstring
extension=php_mbstring.dll
Working like a charm now!

Related

PHP PDO + PostgreSQL Connection Error: Could not find Driver

There are a few similar questions that I have read through and followed the advice but to no end, and only being fairly new to this, I figured now was a good time to stop 'trying' things in case I break anything any further.
I'm receiving the following error when trying to connect to my database via PDO:
Connection error: could not find driver
1. I've ensured that Apache is linked to my homebrew PHP.
$ which php
/usr/local/bin/php
2. Have uncommented the following in php.ini
extension=php_pdo_pgsql.dll
The pdo_pgsql module shows up in php_info
3. My database connection is:
<?php
class Database
{
public $conn;
public function getConnection()
{
try {
$this->conn = new PDO("postgres://$user:$pass#$host:$port/$db_name");
print_r($this->conn);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->exec("set names utf8");
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
exit();
}
return $this->conn;
}
}
I've triple checked these details and they are correct (albeit ommitted). I can connect with the same URI via IntelliJ's Database connection Wizard
4. I’ve edited /usr/local/var/postgres/postgresql.conf to include:
#listen_addresses = '*'
I'm still not having any luck and am looking at some guidance in this head scratcher.
As I see you are using Linux, but tried enable .dll library which is used for Windows machines. It makes sense to comment this line.
Make sure that you have pdo_pgsql module enabled:
# php -m | grep pdo_pgsql
pdo_pgsql
Install it if it is not
# yum install php-pgsql
Here is steps that I did to make PHP+PostgreSQL work on my clean CentOS 7 install:
Install PostgreSQL (but I think you already have this installed and
configured)
# yum install postgresql-server postgresql-contrib
Updated config /var/lib/pgsql/data/pg_hba.conf, changed from ident to md5
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
After
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Restart postgresql service
# service postgresql restart
Install PHP and PDO connector
# yum install php php-pgsql
Here is an example of PHP script I used to test connection:
<?php
// Configure DB Parameters
$host = "localhost";
$dbname = "masterdb";
$dbuser = "automation";
$userpass = "fGmK4hvDZPB6fr6c";
$dsn = "pgsql:host=$host;port=5432;dbname=$dbname;user=$dbuser;password=$userpass";
try{
// create a PostgreSQL database connection
$conn = new PDO($dsn);
// display a message if connected to the PostgreSQL successfully
if($conn){
echo "Connected to the $dbname database successfully!";
echo "\n";
}
}catch (PDOException $e){
// report error message
echo $e->getMessage();
}
And the output:
# php pdo_test.php
Connected to the masterdb database successfully!

Connection failed: could not find driver, PHP7.1 SqlServer, Windows Server 2012R2, IIS

I'm attempting to get PHP 7.1 to connect to an instance of SQL Server on the same network, I get the following error:
Connection failed: could not find driver
I've downloaded and added both php_pdo_sqlsrv.dll and php_sqlsrv.dll (both nts versions). I have tried both the most recent release of, AND, the pre-release versions that supposedly fix an error with the .dll files not working with PHP 7.1.
Both files have been added in our php.ini file as well.
PHP file that should connect to the DB..
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$serverName = "######";
// $myUser = "#####";
// $myPass = "######^";
// $dbName = "######";
//connection to the database
try {
$conn = new PDO("sqlsrv:Server=($serverName,53000);port=1433;Database=$dbName", NULL, NULL);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
?>
The PHP.ini section currently looks like this:
extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
;extension=php_pdo_sqlite.dll
;extension=php_pgsql.dll
;extension=php_shmop.dll
;extension=php_sqlsrv_71_nts.dll
;extension=php_pdo_sqlsrv_71_nts.dll
extension=php_pdo_sqlsrv.dll
;extension=php_pdo_sqlsrv_54_nts.dll
;extension=php_sqlsrv_53_nts.dll
extension=php_sqlsrv.dll
php -info only shows odbc under "PDO drivers" - which I believe indicates that it isn't activating.
Have restarted the server multiple times.
Any help is much appreciated, thanks!

Call to undefined function sqlsrv_connect() - Troubleshooting

System Information
CMS: Wordpress
Web Server: XAMPP
PHP Version: 5.5.30
MS Management Studio 17
Goal
Establish MSSQL Database connection using PHP
What has been done
Downloaded SQLSRV Drivers
Copied files php_pdo_sqlsrv_55_nts.dll and php_pdo_sqlsrv_55_ts.dll to the directory C:\xampp\php\ext
Added the following lines to the dynamic extensions part in the php.ini file: extension=php_pdo_sqlsrv_55_ts.dll and extension=php_pdo_sqlsrv_55_nts.dll
Restarted Web Server
Confirmed sqlsrv is listed in phpinfo()
Code
$serverName = "technology-pc\sqlexpress";
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"example_db");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
} else {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
Error
Call to undefined function sqlsrv_connect()
You have added the PDO variant of SQLSRV drivers to the extension list, but have not added the base drivers php_sqlsrv_55_ts.dll.
Add to the php.ini:
extension=php_sqlsrv_55_ts.dll
or
extension=php_sqlsrv_55_nts.dll
Also, you really should be using either the Thread-Safe (_ts.dll) or Non-Thread-Safe (_nts.dll) versions of the driver, not both. I believe that, as you are using an Apache Server, you should be using the Thread-Safe versions. So you php.ini should have:
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll
You probably edited the wrong php.ini file. Check the right php.ini file with php info.
You can use this script:
<?php echo phpinfo(); ?>
Or if you have CLI access type php -i to get the info listed.
Check for the right path of your php.ini file.
Try below code to connect mssql database
$server = 'dburl.com\MSSQLSERVER, 1433';
$username = 'uname';
$password = 'password';
$connectionInfo = array( "Database"=>$database, "UID"=>$username, "PWD"=>$password,"ReturnDatesAsStrings"=>true);
$conn = sqlsrv_connect( $server, $connectionInfo);

Connection falied: could not find driver

I'm on Windows 10 and I'm using WAMP. I am trying to create a login form with prepared PDO statements. Below is the code to my connect.php script that is issuing the error: "Connection failed: could not find driver".
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$db = 'login';
try{
$conn = new PDO("mysqli:host=$server;dbname=$db", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected Successfully";
}catch(PDOException $e){
echo "Connection falied: " . $e->getMessage();
}
?>
This is a list from my php.ini file that shows and DLLs associated with pdo
extension=php_openssl.dll
extension=php_pdo_firebird.dll
extension=php_pdo_mysql.dll
extension=php_pdo_oci.dll
extension=php_pdo_odbc.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
extension=php_pdo_mysql.dll
extension=php_pdo.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
extension=php_pgsql.dll
;extension=php_shmop.dll
WAMP says "no dll file" for:
-php_mysqli
-php_pdo
I have the "php_pdo" dll file in wamp/bin/php5.6.25/ and in wamp/bin/php5.6.25/ext/ just in case.
I can't find a download for php_mysqli but I don't think I need that for PDO, though it would be nice to have if anyone has a download link.
So my question is, did I just install the dll file into the wrong directory, or what? I'm not sure what to do, But I just installed WAMP and I can't use PDO. Should I use another client(I'm not sure what they are called).
I think you have a typo, try mysql instead of msqli:
$conn = new PDO("mysql:host=$server;dbname=$db", $username, $password);
Also from comments:
Try to put this in the php.ini : extension_dir = "c:\php5\ext" and point to the extension
move php_pdo.dll before the other PDO extensions, and restart WAMP
Edit: I decided to change my answer to be a community wiki, since I feel there should not be any rep that should come of this. Plus, the question being a typo on more than one count.
Original answer:
The syntax is:
mysql:host
and not mysqli. I hope this isn't another "typo" as per your original post https://stackoverflow.com/revisions/40444027/1 of new PDO("msqli:host
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
As per the manual:
http://php.net/manual/en/pdo.connections.php
Note: Wamp should be ready to use "out of the box" and not require any drivers to be added.
I use Wamp also and xampp and never had a problem.
Plus, Wamp installation comes in different flavours (32 and 64 bit), so make sure you chose the right one.
http://www.wampserver.com/en/
You can also try installing xampp
https://www.apachefriends.org/
Use error reporting also, which would have been of help here:
http://php.net/manual/en/function.error-reporting.php

Cannot connect to MYSQL using PHP

I could not connect to the local host MYSQL database using my simple php code and DOES NOT display anyything. THis is applicable to all users and root.Eventhough the problem is reported many times, i could not fix it. Also it is written in one site that it may have to make changes in php file.Please give me solution for this connection error.
I use php 5.5.9-1 in Ubuntu. MYSQL version is 5.5.38.
My code is
<?php
$servername = "localhost";
$username = "root";
$password = "*****";
$conn = mysql_connect($servername, $username, $password);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Call to undefined function mysql_connect()
This error message from your comments tells me that you do not have the MySQL extension installed.
Execute this in your terminal to install: sudo apt-get install php5-mysql
Then restart Apache: sudo /etc/init.d/apache2 restart
Please be aware that the "mysql_" functions are marked as deprecated and your script might fail with a future PHP update. Please use "mysqli_" or PDO.
Mysql is deprecated. Use Mysqli.
Create a file and name it as db_connect.php and copy the below code.
<?php
$connection = mysqli_connect("localhost","username","password","database_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Open browser and type http://localhost/db_connect.php. If you see a blank page, your connection is fine. If there's a problem, it will show you the error.

Categories