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!
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!
Basically the title,
When I attempt to connect to a database I get the error: could not find driver.
I've searched online and it was said I had to uncomment several extensions.
I uncommented extension=php_pgsql.dll and extension=php_pdo_pgsql.dll in the php.ini in C:\xampp\php, but that didnt solve it.
When I run my code on the online server the database connection works.
I'm using PDO for the database to a MSSQL server.
Basic information:
I'm using XAMPP Control Panel V3.2.2 on Windows
I connect like so:
define("USER_NAME", "myusername");
define("DATABASE", "mydatabase");
define("PASSWORD", "mypassword");
define("HOST", "myhost");
try{
$db = new PDO("dblib:host=".HOST.";dbname=".DATABASE, USER_NAME, PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $exception){
echo $exception->getMessage();
exit;
}
EDIT
I downloaded MSSQL driver for PDO.
I added these two extensions:
extension=php_sqlsrv_54_ts.dll
extension=php_pdo_sqlsrv_54_ts.dll
Without success.
-EDIT-
You enabled the wrong extension to use MSSQL database.
So you could check the dblib docs for the dll (tip from https://stackoverflow.com/users/934966/jon-stirling).
-ORIGINAL-
You forgot the database server port by the host (host=".HOST.":".PORT.";):
define("USER_NAME", "myusername");
define("DATABASE", "mydatabase");
define("PASSWORD", "mypassword");
define("HOST", "myhost");
define("PORT", "10060");
try{
$db = new PDO("dblib:host=".HOST.":".PORT.";dbname=".DATABASE, USER_NAME, PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $exception){
echo $exception->getMessage();
exit;
}
Hope this works!
More information:
http://php.net/manual/en/ref.pdo-dblib.php
You can't use PDO_dblib in XAMPP3.2.2
This extension is not available anymore on Windows with PHP 5.3 or later.
On Windows, you should use SqlSrv
I am having a trouble to connect to MS SQL2000 with PHP through COM library.
The message I am getting is:
Could not establish a database connection exception 'com_exception'
with message 'Source: Microsoft OLE DB Provider for ODBC Drivers
Description: [Microsoft][ODBC SQL Server Driver][DBMSLPCN]SQL Server
does not exist or access denied.....
I have read through many posts and couldn't find any straight forward sample how to setup PHP in order to connect easily. These are the steps I've taken so far:
1) running MSSQL2000 with TCP/IP and Named Pipes enabled, listening on port 1433
2) installed PHP Version 5.4.16 TS
3) I have the following extensions enabled (related to mssql):
extension=php_com_dotnet.dll
extension=php_pdo_sqlsrv_53_ts_vc9.dll
extension=php_sqlsrv_53_ts_vc9.dll
extension=php_pdo_mssql.dll
extension=php_pdo_odbc.dll
4) I have checked for the COM windows permissions
5) I am sure I have provided the correct database/instance, username, password for the connection string.
6) when I instantiate the COM object with:
$connection = new COM("ADODB.Connection");
print_r($connection);
...I get the "com Object" output response, meaning the class seems to work fine.
What have I overlooked ? I have to connect through ADODB since the whole site I am working on is build on it. Is there a way to debug this? Any help is appreciated.
PROBLEM SOLVED!
After a few days of figuring this out and reinstalling virtually everything including Apache, different PHP and MSSQL versions I have come up with the following. I am not claiming this to be the best solution but it works:
Functional environment:
1) Windows Azure Server with MSSQL 2012
2) Apache 2.2 PHP 5.3.28 - installer: php-5.3.28-Win32-VC9-x86.msi downloaded from:
http://windows.php.net/download/
PHP Extensions (I am listing all but you need just the last few):
[PHP_BZ2]
extension=php_bz2.dll
[PHP_CURL]
extension=php_curl.dll
[PHP_ENCHANT]
extension=php_enchant.dll
[PHP_FILEINFO]
extension=php_fileinfo.dll
[PHP_GD2]
extension=php_gd2.dll
[PHP_GETTEXT]
extension=php_gettext.dll
[PHP_GMP]
extension=php_gmp.dll
[PHP_IMAP]
extension=php_imap.dll
[PHP_INTL]
extension=php_intl.dll
[PHP_LDAP]
extension=php_ldap.dll
[PHP_MBSTRING]
extension=php_mbstring.dll
[PHP_MYSQL]
extension=php_mysql.dll
[PHP_MYSQLI]
extension=php_mysqli.dll
[PHP_OCI8]
extension=php_oci8.dll
[PHP_OCI8_11G]
extension=php_oci8_11g.dll
[PHP_OPENSSL]
extension=php_openssl.dll
[PHP_PDO_MYSQL]
extension=php_pdo_mysql.dll
[PHP_PDO_OCI]
extension=php_pdo_oci.dll
[PHP_PDO_ODBC]
extension=php_pdo_odbc.dll
[PHP_PDO_PGSQL]
extension=php_pdo_pgsql.dll
[PHP_PDO_SQLITE]
extension=php_pdo_sqlite.dll
[PHP_PGSQL]
extension=php_pgsql.dll
[PHP_SHMOP]
extension=php_shmop.dll
[PHP_SNMP]
extension=php_snmp.dll
[PHP_SOAP]
extension=php_soap.dll
[PHP_SOCKETS]
extension=php_sockets.dll
[PHP_SQLITE]
extension=php_sqlite.dll
[PHP_SQLITE3]
extension=php_sqlite3.dll
[PHP_SYBASE_CT]
extension=php_sybase_ct.dll
[PHP_TIDY]
extension=php_tidy.dll
[PHP_XMLRPC]
extension=php_xmlrpc.dll
[PHP_XSL]
extension=php_xsl.dll
[PHP_EXIF]
extension=php_exif.dll
php_sqlsrv_53_ts_vc9.dll
php_pdo_sqlsrv_53_ts_vc9.dll
extension=php_com_dotnet.dll
The php_pdo_sqlsrv_53_ts_vc9.dll extension pack was downloaded from: http://www.microsoft.com/en-us/download/details.aspx?id=20098
The extension=php_com_dotnet.dll downloaded from: http://originaldll.com/file/php_com_dotnet.dll/29343.html (cannot verify how secure this file is but it works)
The MSSQL 2012 needs to be set as follows:
when creating your database, open the management studio and right click the database instance in the left tree. Make sure that under the properties->security tab you select: "SQL Server and Windows Authentication Mode". Without this you will never connect if you are using the standard username / password method.
Then proceed with creating the database schema and under the main security tab create the new user with username/password related to this database. Make sure you set all permission right i.e: owner, datareader, datawriter.
The PHP connection script I used is as follows:
*===============================================*
function openConnection() {
$dbaseServer = "instance name";
$dbaseIp = "127.0.0.1";
$dbasePort = "1433";
$dbaseName = "databsse name";
$dbaseUser = "username";
$dbasePassword = "password";
$connectionString = ""
."Driver={SQL Server};"
."Network=DBMSSOCN;"
."Server=$dbaseServer;"
."Address=$dbaseIp:$dbasePort;"
."Database=$dbaseName;"
."Uid=$dbaseUser;"
."Pwd=$dbasePassword";
try {
$connection = new COM("ADODB.Connection");
$connection->ConnectionTimeout = 60;
$connection->Open($connectionString);
return $connection;
} catch (exception $e) {
echo "Could not connect - $e";
exit;
}
}
// test query
//===========
$sql = "SELECT * FROM [dbname].[dbo].[tablename]";
$connection = openConnection();
// unnecessary but in my case more time is needed.
$connection->CommandTimeout = 240;
$recordset = $connection->Execute($sql);
if (!$recordset) { $connection->Close(); die("Request failed!"); }
if (!$recordset->EOF) {
while (!$recordset->EOF) {
$test[] = $recordset[0]->value;
$recordset->MoveNext();
}
}
print_r($test);
$connection->Close();
--
I think that's it. Please feel free to correct my post and/or improve it :)
This question already has answers here:
Fatal error: Uncaught Error: Call to undefined function mysql_connect()
(9 answers)
Closed 3 months ago.
I have set up PHP, MySQL, and Apache. localhost() for PHP and it is working well. But after I downloaded MySQL, it reports:
Fatal error: Call to undefined function mysql_connect()
How can I fix this?
You upgraded to PHP 7, and now mysql_connect is deprecated. Check yours with:
php -version
Change it to mysqli_connect as in:
$host = "127.0.0.1";
$username = "root";
$pass = "foobar";
$con = mysqli_connect($host, $username, $pass, "your_database");
If you're upgrading legacy PHP, now you're faced with the task of upgrading all your mysql_* functions with mysqli_* functions.
If you get this error after upgrading to PHP 7.0, then you are using deprecated libraries.
mysql_connect — Open a connection to a MySQL Server Warning This
extension was deprecated in PHP 5.5.0, and it was removed in PHP
7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
More here: http://php.net/manual/en/function.mysql-connect.php
Open your terminal and run bellow command.
sudo apt-get install mysql-server
If you are running PHP you will also need to install the php module for mysql 5:
sudo apt-get install php5-mysql
Verify that your installation of PHP has been compiled with mysql support. Create a test web page containing <?php phpinfo(); exit(); ?> and load it in your browser. Search the page for MySQL. If you don't see it, you need to recompile PHP with MySQL support, or reinstall a PHP package that has it built-in
PHP.INI
Check if you forgot to enable the options below(loads the modules for mysql among others):
; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = "ext"
Use.
<?php $con = mysqli_connect('localhost', 'username', 'password', 'database'); ?>
In PHP 7.
You probably have PHP 7 in XAMPP. You now have two option: MySQLi and PDO.
For CPanel users, if you see this error and you already have PHP 5.x selected for the site, there might be a CPanel update that disabled mysql and mysqli PHP extensions.
To check and enable the extensions:
Go to Select PHP Version in CPanel
Make sure you have PHP 5.x selected
Make sure mysql and mysqli PHP extensions are checked
This error is coming only for your PHP version v7.0.
you can avoid these using PHP v5.0
else
use it
mysqli_connect("localhost","root","")
i made only mysqli from mysql
A solution could be to use adapter functions like these to start using mysqli instead of mysql over your entire application:
if (!function_exists('mysql_connect')) {
function mysql_connect($host = '', $user = '', $password = '', $database = '', $port = 0, $socket = '') {
return mysqli_connect($host, $user, $password, $database, $port, $socket);
}
}
if (!function_exists('mysql_select_db')) {
function mysql_select_db($link, $dbname) {
mysqli_select_db($link, $dbname);
}
}
I had this same problem on RHEL6. It turns out that the mysql.ini file in /etc/php.d only had a module name but needed a full path name. On my RHEL6 system the entry that works is:
; Enable mysql extension module
extension=/usr/lib64/php/modules/mysql.so
After modifying the file, I restarted apache and everything worked.
My solution is here (I needed just to remove the last slash (NB: backward slashes) from PHPIniDir 'c:\PHP\'): Fatal error: Call to undefined function mysql_connect() cannot solve
Am using windows 8 n this issue got resolved by changing the environment variables
follow these steps:
Open my computer properties->advanced system settings->Environment variables.
Under 'system variables', select 'path' and click on 'edit'
In 'variable value', add 'C:\php;' OR the path where php installed.
click OK and apply the settings and restart the system.
It should work.
Here is a quick fix:
All the pros will probably hate me for this answer. But I got the same error on a server: Fatal error: Uncaught Error: Call to undefined function mysql_connect() that was using PHP 7. Did not have time to rewrite all the mysql code so a quick, temporary fix if anyone needs it is in CPANEL to look for PHP Configuration and change the version for that account to something like PHP 5.4 instead of PHP 7. Then the code worked fine without the above error.
If you are using Windows10, PHP 7.2 and try to connect to mysql.
If this error occurred
Uncaught Error: Call to undefined function mysqli() in
The do the following steps to get it correct.
Go to the PHP installation folder,
CHeck for php.ini file, (Only dev, prod file is there, then one of the file as php.ini file)
Look for "extension=mysqli" and remove the ";" before it.
Look for "extension_dir" and mentioned the path of "ext" directory.
Restart the application.
Hope this helps to someone.
Check if mysqli module is installed for your PHP version
$ ls /etc/php/7.0/mods-available/mysql*
/etc/php/7.0/mods-available/mysqli.ini /etc/php/7.0/mods-available/mysqlnd.ini
Enable the module
$ sudo phpenmod mysqli
This Code Can Help You
<?php
$servername = "localhost";
$username = "root";
$password = "";
$con = new mysqli($servername, $username, $password);
?>
But Change The "servername","username" and "password"
The problem shows up when you're using mysql_connect in your code lines so just add it as in mysqli_connect and it will solve the problem.
You also have to then use mysqli throughout your code lines or the problem would surface again.
The example mysql_select_db will then be mysqli_select_db for selecting Database.