Connecting to MSSQL server via php-pdo? - php

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

Related

pdo cannot find driver and no php_pdo extension

When I tried to put my website on localhost, the php file that connects it to a mydql datdabase says cannot find driver.
I searched up the problem and found other people with the same problem and they solved it by uncommenting an extension in their php.ini file. I tried to do the same but php_pdo.dll isnt in my php.ini file. what should i do?
if ur wondering, heres the code that connects to mysql.
<?php
try {
$pdo = new PDO("mysql:host=localhost", "105th time lucky", "password");
// Set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Print host information
echo "Connect Successfully. Host info: " .
$pdo->getAttribute(constant("PDO::ATTR_CONNECTION_STATUS"));
} catch (PDOException $e) {
die("ERROR: Could not connect. " . $e->getMessage());
}
?>

Connecting SQL server DB to A PHP web application using Wamp

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

DBMaker connection with PHP ODBC (using PDO)

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.

Connection error to SQL Server via PHP, SQL STATE IM004, Driver's SQLAllocHandle on SQL_HANDLE_ENV failed

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.

Unable to connect to Google Cloud SQL using PDO

I'm a PHP newbie. I'm trying to connect to GAE using PHP and PDO. I have tried connecting using mysql_connect() and mysql_select_db(). I have been successful. However when I try to connect using PDO, I get an error
Connection failed: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
My code is as follows:
define('DBH_SOCKET', '/cloudsql/****:****');
define('DBH_NAME', 'wordpress_db');
define('DBH_USER', 'censored');
define('DBH_PASSWORD', 'censored');
$pdo_conn = "mysql:unix_socket=".DBH_SOCKET.";dbname=".DBH_NAME.";charset=utf8";
try {
$dbconn = new PDO($pdo_conn, DBH_USER, DBH_PASSWORD);
$dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch (PDOException $e){
echo 'Connection failed: ' . $e->getMessage();
}
echo "<br> var is ".$pdo_conn;
/*$conn = mysql_connect(DBH_SOCKET, DBH_USER, DBH_PASSWORD);
if (!$conn)
die('Connect error ('.mysql_error());
$db_selected = mysql_select_db(DBH_NAME, $conn);
if(!$db_selected)
die('Cant use db: '.mysql_error());*/
What am I doing wrong? I have looked at some tutorials on the net like, http://webandphp.com/WorkingwithPHPontheGoogleCloudPlatform-166942. They use PHPStorm and a JDBC driver. However, there is no mention of using a JDBC driver in the official google tutorials.
When connecting using PDO locally, you'll need to use a connection string that matches your local MySQL server settings, e.g. "mysql:host=localhost;dbname={db name};charset=utf8".
When running on GAE, your current connection string should work fine. The fact that you're getting the error message "Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?" suggests that you might have missed the "unix_socket=/cloudsql/{project id}:{instance id}" part.

Categories