I have searched through many posts trying to find an answer but get nowhere.
I am trying to establish a connection to and Sql Server DB through my PHP web application using WAMP.
What I have tried:
I downloaded the sql drivers for PHP 7 and 7.1 and tried them with the corresponding PHP versions
I made sure to restart all services after updating the php.ini file.
I haveinstalled the SQLSRV40.EXE and updated the php.ini with:
extension=php_pdo_sqlsrv_7_ts_x64.dll
extension=php_pdo_sqlsrv_7_nts_x64.dll
I did not though that even though these are added in the php.ini they were not in the php> php extentions list - not sure why
This is my code below allow with the error
<?php
$serverName="DESKTOP-0KNJ0KP";
$connectionInfo=array("Database"=>"SPMS_db",);
$conn=sqlsrv_connect($serverName,$connectionInfo);
if ($conn) {
echo "Connected.<br />";
} else {
echo "Connection failed.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
I have added context fro PHPinfo()
You have installed PDO_sqlsrv part of PHP Driver for SQL Server, but your code uses sqlsrv functions. You have two options:
install php_sqlsrv_ extensions to make these functions work or
rewrite your code to use PDO version of the driver
PHP code using PDO version of PHP Driver for SQL Server:
<?php
# Connection
$server = "DESKTOP-0KNJ0KP";
$database = "SPMS_db";
try {
$conn = new PDO( "sqlsrv:Server=$server;Database=$database", NULL, NULL);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch( PDOException $e ) {
die( "Error connecting to SQL Server. ".$e->getMessage() );
}
# End
$conn = null;
?>
Connecting through PDO library (which is already installed in your server) is easier.
See PDO Book from PHP.NET: https://www.php.net/manual/en/ref.pdo-dblib.php
Related
Im trying to connect to a Azure SQL server but get the following exception:
PDOException Object ( [message:protected] => could not find driver
After some research i found that most people run into this problem when the pdo_mysql extension isnt activated in their php.ini
So i checked my php.ini file but i already enabled it and it works otherwise Laravel wouldnt work for my other projects.
Then i found out that you have to check your phpinfo(). But when checking my phpinfo i can see that the driver is installed.
I am trying to connect to the SQL server using the following PHP code:
$sqlServer = "tcp:server.database.windows.net,1433";
$sqlUser = "admin#server";
$sqlPwd = "password";
$sqlDb = "database";
$sqlDsn = "sqlsrv:Server=$sqlServer;Database=$sqlDb";
try {
$conn = new PDO($sqlDsn, $sqlUser, $sqlPwd);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
die(print_r($e));
}
for asking this question i've changed the credentials, i've triple checked and the credentials i'm using are correct.
Can someone help me figure out why i get the driver exeption?
Could you please make sure you have installed this driver?
After that make sure you have enable it on php_ini by adding the line:
extension=php_pdo_sqlsrv_72_nts.dll
After that please restart Apache.
I'm developing an application with a php framework (Yii) who use pdo connection also for remote ODBC databases.
I'm working on a Debian server with PHP 7.1, with DBMaker drivers installed, ODBC.ini configured so DBMaker works well with this code:
if(!odbc_connect('MYDBDSN','myuser', 'mypass')) {
echo "Connection not found " . odbc_errormsg();
} else {
echo "Database exists";
}
// queries works!
But a connection made by
$conn = new PDO ("odbc:MYDBDSN", 'myuser', 'mypass');
says Unable to connect; PDOException: could not find driver.
Is there any way to connect my DBMaker database with PDO, making my queries framework compatible?
I needed to install and configure pdo_odbc drivers for PHP.
This link can be useful if your servers works under a Docker environment.
I already have searched and googling about my question, but i still have not find the answer. My problem is when connecting to my SQL Server database via PHP PDO/ODBC Connection, i always getting error : "[Microsoft][ODBC Driver Manager] Driver's SQLAllocHandle on SQL_HANDLE_ENV failed, SQL state IM004 in SQLConnect". But my connection to Oracle or MySQL Database are not problem at all, only to SQL Server database.
Here my code to testing connection :
$dbh = null;
try
{
$dbh = new PDO('oci:dbname='.TNS, DB_USERNAME, DB_PASSWORD, null);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $pe) {
//var_dump($pe->getMessage());
}
if (!is_null($dbh)) {
echo "Oracle Connection success";
}
else {
echo "Oracle cannot connect";
}
echo "<br />";
$dbh = null;
try
{
$dbh = new PDO('mysql:host=localhost;dbname=db_test', 'root', '', null);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $pe) {
//var_dump($pe->getMessage());
}
if (!is_null($dbh)) {
echo "MySQL Connection success";
}
else {
echo "MySQL cannot connect";
}
echo "<br />";
$dbh = null;
try
{
$dbh = new PDO('sqlsrv:server=XXXXX;Database=xxxxxxx', 'xxxx', 'xxxx', null);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $pe) {
//var_dump($pe->getMessage());
}
if (!is_null($dbh)) {
echo "MySQL Connection success";
}
else {
echo "SQL Server cannot connect";
}
The results above is shown as :
Oracle Connection success
MySQL Connection success
SQL Server cannot connect
I have tried to reinstall XAMPP and copying the new PHP's SQL Server library. But the error still same. As i mention above, i have been searching the solution, but still no luck. I truly hope, i can get a solution for my problem, or at least a clue for me to started with. Oh yeah, i almost forgot, this error occurs after Windows 10 update, before that, my connection to SQL Server is fine. So, i wonder whether this problem related to Windows update or not. I really have no clue.
Sorry for my English language, and thank you very much for your helps. I really apreciated for any answer
I was having the same issue and these are the steps I took to make sure it was working correctly:
My Config (Win10, XAMPP, PHP 7.2.8)
1) Downloaded the Microsoft PHP Drivers for SQL Server 5.3 and put the following files in the C:\xampp\php\ext directory:
php_sqlsrv_72_ts_x86.dll
php_pdo_sqlsrv_72_ts_x86.dll
2) Added the following lines to my php.ini file in c:\xampp\php
extension=php_pdo.dll
extension=php_sqlsrv_72_ts_x86.dll
extension=php_pdo_sqlsrv_72_ts_x86.dll
Note: I was originally missing the first line (extension=php_pdo.dll) and that gave me a "Driver's SQLAllocHandle on SQL_HANDLE_ENV failed" error. You do not need to put the dll in the ext directory because in PHP 7 it is built-in but you do need to put the line in the ini file.
3) Downloaded and installed Microsoft ODBC Driver for SQL Server 17.
Make sure you stop and restart the Apache server after making the changes.
I'm unable to connect to MSSQL database on smarterasp.net domain. I'm using:
PHP Tools for Visual Studio
Here is the code:
<?php
try {
$conn = new PDO("mssql:host=host_name_string;dbname=database_name_string", "username_string", "password_string");
// 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();
}
?>
and the error caught by the PDOexception class:
Connection failed: could not find driver
I've also tested my code online and again getting the same error(online tester url: http://sandbox.onlinephpfunctions.com).
I've searched for the solution and the only thing that could resolve my problem is by uncommenting:
extension=php_pdo_mysql.dll
but the line is already uncommented by default.
EDIT:
The following does not ressolve my problem (instead of mssql:host)
sqlsrv:host
dblib:host
Your problem is that you have not installed either the sql server client or the Microsoft pdo drivers on your machine. Please do that and make sure you can connect via a udp file.
Search SQL server client install and Microsoft pdo drivers
I am trying to use Microsoft server in a PHP program. I downloaded the Microsift drivers for PHP for SQL Server, made sure that Microsoft SQL Server 2012 Client was installed, made sure the extension_dir value was correct in my PHP.ini file, and added extension=php_sqlsrv_53_ts.dll in the dyanmic extension section of my PHP.ini file. Yet i still get the error 'Fatal error: Call to undefined function sqlsrv_connect()'. I am running an apache server on windows 7 and downloaded the 3.0 version of the driver.
So what am I doing wrong? What else should I check for? If I am running under an Apache server, I don't need to do anything to IIS, do I? Below is my PHP code.
<?php
// phpinfo(INFO_MODULES);
require_once 'serverlogin2.php';
/* Specify the server and connection string attributes. */
//$connectionInfo = array ("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd);
$connectionInfo = array ("Database"=>$databaseName);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
echo "made sql connection";
return;
?>
sqlsrv_connect (sqlsrv extension) is not available in the GA branch, you need to get it from svn and compile it. You can also use the mssql_connect (mssql extension) or PDO.