Could not find driver when connect PHP to SQL SERVER - php

I want to connect to SQL Server at another pc with php, when i try to connect it says
Connection error: could not find driver
What I have tried is add extension in php.ini.
;extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_72_ts_x64.dll
extension=php_sqlsrv_72_ts_x64.dll
my PHP version is: 7.2.3
SQL SERVER: SQL Server 2014
OS PC: Windows Server 2012 R2 (another pc)
here's my code:
<?php
$host = "192.168.3.126";
$db_name = "dbo";
$username = "david";
$password = "david";
try {
//$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
$db = new PDO("sqlsrv:Server={$host};Database={$db_name}", $username, $password);
}
catch(PDOException $exception){ echo "Connection error: " . $exception->getMessage();}
?>
NB: I have download Microsoft Drivers 5.6 for PHP for SQL Server at here, I have download install the ODBC Driver 17 at my own pc
I have tried this reference but it doesn't work PHP Sql Server PDOException:could not find driver

Related

Howto connect with PHP to a MS SQL Server within a Docker setup?

I am trying to connect to a MS SQL server with PHP in a Docker environment.
I followed this instructions for my Dockerfile:
FROM php:7.3.10-apache
RUN apt-get -y install unixodbc-dev
RUN pecl install sqlsrv pdo_sqlsrv
The following two lines are added to the php.ini:
extension=pdo_sqlsrv.so
extension=sqlsrv.so
The phpinfo() shows this:
Now I am trying to connect to the MS SQL Server (which is not part of the docker environment):
$server = 'MyServer';
$database = 'MyDB';
$username = 'MyUser';
$password = 'MyPassword';
# Connect
try {
$conn = new PDO("sqlsrv:server=$server;Database=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("Error connecting to SQL Server: ".$e->getMessage());
}
But I am getting the following error:
Error connecting to SQL Server: SQLSTATE[IMSSP]: This extension
requires the Microsoft ODBC Driver for SQL Server to communicate with
SQL Server. Access the following URL to download the ODBC Driver for
SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712
Can someone point me in the right direction? What exactly do I need to do?

Connecting PDO to MS SQL Server 2008 R2 Named Pipes Provider: Could not open a connection to SQL Server [53]

My PC has Installed MS SQL Server 2008 R2 with installed Database, Then I installed XAMPP and made another port for Apache to make it localhost:8080 and listen to 4433. Then Installed OBDC, Tried to connect using
try {
$host = 'LOCAL';
$user = '****';
$pass = '****';
$dsn = "sqlsrv:Server=$host;Database=$db;";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$pdo = new PDO($dsn, $user, $pass, $opt);
} catch (PDOException $e) {
echo "No connection: " . $e->getMessage();
exit;
}
But I keep getting
No connection: SQLSTATE[08001]: [Microsoft][ODBC Driver 13 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53].
Used extension=php_pdo_sqlsrv_71_ts_x86.dll
I've shutdown Windows Firewall, Enabled Pipe Connection in MS SQL 2008 Configuration and Internet Option NetBIOS Enabeled
$host shouldn't be "LOCAL" but "localhost".
If you are running SQL server on a custom port on your local machine, you can specify the port by appending it with a comma to the host; applied to your code snippet:
$host = "localhost";
// or, with a custom port
$host = "localhost,12345"
Source: http://php.net/manual/en/ref.pdo-sqlsrv.connection.php#refsect1-ref.pdo-sqlsrv.connection-examples
if you are using a local instance, the server string needs a double backslash:
<?php
$conn = new PDO('sqlsrv:Server=localhost\\SQLEXPRESS;Database=MyDatabase', 'MyUsername', 'MyPassword');
?>
See original provided answer by Daniel Klein:
https://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php#121301

connecting to SQL Server 2008 using php pdo

I am working with the SQL Server 2008 and php pdo. I am trying to connect from php to sql server 2008.
my php version is:5.5.12
I am getting error :: could not find driver
what I tried is to connect to sql server 2008 database from php like:
<?php
$dbhost = "localhost";
$dbname = "xxxxxx";
$dbuser = "xx";
$dbpass = "";
try {
$con = new PDO("sqlsrv:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
I also surfed a lot but did not found the solution.
my extensions in php.ini
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll
location where i put the dll is C:\wamp\bin\php\php5.5.12\ext

sqlsrv connection string not valid [87]

Set up:
Wamp Server
PHP 5.5.12
Apache 2.4.9
MS SQL Server 2012
Already completed:
Installed (unofficial) php_sqlsrv_55_ts and php_pdo_sqlsrv_55_ts AND confirmed they are working via phpinfo() sqlsrv_link
I have made sure that TCP/IP is enabled in SQL Configuration Manager
I have tested credentials in SQL Management Studio AND via ODBC in Administrrative Tools
IIS is disabled
Additionally, I did have trouble with SQL Reporting Service taking port 80. This was giving me a problem with Apache, so I directed SQL Reporting Service to use port 8081.
<?php
$server = "computer_name\MSSQLSERVER";
$user = "sa";
$pass = "password";
$db = "pcm";
$connInfo = array("Database"=>$db, "UID"=>$user, "PWD"=>$pass);
$conn = sqlsrv_connect($server, $connInfo) or die( print_r( sqlsrv_errors(), true));
?>
If you are using the PDO extension, you will have to use a PDO connection instead of the sqlsrv_connect() function. Microsoft doesn't support the UID or PWD keys that you are providing when trying to connect via the PDO extension. Try this instead:
$conn = new PDO('sqlsrv:Server = ' . $server . '; Database = ' . $db, $user, $pass);
http://php.net/manual/en/ref.pdo-sqlsrv.connection.php

Using a FreeTDS ODBC driver to connect Linux PHP to a SQL Server

I just set up my freetds.conf file with a new virtual account to connect to a SQL Server database but I am not sure when and where I tell my PHP script which database to connect to below are my settings
odbc.ini
[McDo]
ServerName = server1
Driver = FreeTDS
Description = MyServer
Trace = Yes
freetds.conf
[server1]
host = 66.111.xxx.xxx
port = 1433
tds version = 7.0
And here is my PHP connect script. Now sure where I tell the script to connect to which database.
putenv('ODBCINI=/etc/odbc.ini');
$connect = odbc_connect("server1", "username", "password");
//$query = "SELECT name_ID FROM ext_name";
if(!$connect){
echo "not connected";
}else{
echo "connected";
}
odbc_close($connect);
The php manual shows:
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);

Categories