SQL Server PDO could not find driver - php

I'm using Microsoft Azure SQL for my database and I'm trying to connect it to my project on my localhost using XAMPP. When I try to connect to the database using the connect string they provided:
try {
$conn = new PDO("sqlsrv:server = tcp:app.database.windows.net,1433; Database = mydatabase", "{myusername}", "{your_password_here}");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
print("Error connecting to SQL Server.");
die(print_r($e));
}
I keep getting this error:
Error connecting to SQL Server.PDOException Object ( [message:protected] => could not find driver
The error is longer but I think it's related to the driver mentioned at the end. I'm using a mac so any driver I need to install would need to be compatible with it. Thank you for the help in advance.

Azure SQL Database is built on the Microsoft SQL Server engine. So you might consider using pdo-dblib to connect to SQL Server using the PDO on a Mac. You can check out a Stack Overflow question about it here or refer to this setup documentation.
After the driver having been installed, Azure SQL won’t actually accept connection from your local yet. And you might get the following error:
Client with IP address '167.xxx.xxx.xxx' is not allowed to access the
server.
That’s because there is a firewall in the way by default. To enable access, go to the Azure portal, click on All Resources, select your SQL service, click on Firewall in the SETTING menu.
Your client address is conveniently included in the list, so you can just click on Add client IP followed by Save.
Well, when you run your code now, it should connect.

Related

Azure - PHP Connect to SQL Server

I want to connect with SQL Server using PHP on Azure.
My code works successfully on my host, but the following issues occur after I deploy to Azure application service:
SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [5]
Code:
try {
$conn = new PDO("sqlsrv:Server=1xx.xx.xx.xx;Database=MyDB", "username", "password");
} catch (PDOException $e) {
echo $e->getMessage();
}
1xx.xx.xx.xx is another remote server, I want to connect to this server from Azure.
And phpinfo.php:
PDO drivers : mysql, sqlsrv, pgsql
So I should be able to use the PDO sqlsrv function.
What do I need to set up on the Azure application service?
Before you deploy your PHP code to Azure APP Service, I think you need to test if you PHP code can connect to the remote SQL Server on local computer successfully.
If you want to connect to remote SQL server, you must make sure that:
1. make sure you have allow network connection from sql server configuration tool
2. allow connection for this port in firewall
3. activate sql browser service
4. make sure port is listen as the the service provide
Reference:
Connecting remote SQL Server to PHP.
How to connect to a remote SQL Server.
If it can runs correctly with no error on local computer, then deploy you app to Azure application service.
Here are two examples about connect to the MSSQL:
1.connect to a specified MS SQL Server database:
$c = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");
2.connect to a MS SQL Server database on a specified port:
$c = new PDO("sqlsrv:Server=localhost,1521;Database=testdb", "UserName", "Password");
Hope this helps.

PDO statement in Azure websites with MySQL in-App

I have created a web app to communicate with MySQL In-App for Azure website.
However I'm getting error as,
"Connection failed: SQLSTATE[HY000] [2002] An attempt was made to access a socket in a way forbidden by its access permissions."
I have made sure that host, database, username and password are correct.
Don't know the issue, looks like authentication issue though.
Sample Code :
$pdocon = "mysql:host".$conStr["Data Source"].";dbname=".$conStr["Database"];
try {
$conn = new PDO($pdocon, $conStr["User ID"], $conStr["Password"]);
// 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();
}
Where, if I simply print $pdocon I have values.
Also, I have correct values in my $conStr array.
If you are using lower App Service Plan, you may scale to a higher App Service Plan and then check.
Check in your web app application settings if there is a connection string. PHPmyadmin uses MYSQLCONNSTR_ to connect to the MySQL server. If you have a connection string in application setting change the connection string type to Custom , so you can still have the information if needed or delete it. This will force PHPmyadmin to access MYSQLCONNSTR_localdb and connect to the MySQL in-app server.
Reference:https://blogs.msdn.microsoft.com/appserviceteam/2016/09/08/troubleshooting-faq-for-mysql-in-apppreview/
As mentioned in this document (https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox), Connection attempts to local addresses (e.g. localhost, 127.0.0.1) and the machine's own IP will fail, except if another process in the same sandbox has created a listening socket on the destination port.
Rejected connection attempts, such as the following example which attempts to connect to 127.0.0.1:80, will result an exception error.
Refer these FAQs (https://github.com/projectkudu/kudu/wiki/MySQL-in-app) for more details:
Where can I find MySql Credentials (connection string)?
The connection string flows to your application as an env variable MYSQLCONNSTR_localdb. Beware that we are not using the default MySql port (3306). In fact, the port number may vary for each application life cycle depending on its availability at startup time. The port info is also available as an env variable WEBSITE_MYSQL_PORT to your site.
How to use phpMyAdmin with MySql in-app?
phpMyAdmin is enabled by default with the feature. You can access it thru https://.scm.azurewebsites.net/phpMyAdmin/. Since MySql is only started with the main site, do make sure that the main site is running (simplest way is to turn on AlwaysOn) before using phpMyAdmin. Unlike phpMyAdmin from SiteExtenions gallery, this phpMyAdmin is aware of MySql credentials and will connect automatically.
Important: If you previously have phpMyAdmin installed via SiteExtension gallery, you will have to uninstall it. Since this phpMyAdmin from SiteExtension gallery will take precedent and it is notMySql In-App aware, it will not work with MySql In-App.
Maybe the values for connection string are not correct. That message is related to a bad username, password, database or server values.
Try to check this link if you are obtaining the connection values in another form. The connection string is stored under D:\home\data\mysql\MYSQLCONNSTR_localdb.txt
Another option can be related to stopped MySQL process -only for free app service plan- in such case you should run any PHP file that fires the MySQL process.

Unable to perform any query to Mysql server on Azure from local php server

I am able to establish connection to mysql on Azure from local server. Then I am trying to get data from users table but it is failing. I tried using mysqli and also pdo connections. In any case it is failing. Below is the screenshot of failure. Please give me a solution.
I am using Laravel 5.5.
In laravel controller :
if (DB::connection()->getDatabaseName())
{
return 'Connected to the DB: ' . DB::connection()->getDatabaseName();
}
this connection is established successfully and returning db name
But when tried to query like this, it is throwing error:
if (DB::connection()->getDatabaseName())
{
return DB::select('select * from `users`');
}
ERROR MESSAGE :
Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] (SQL: select * from `users`) in file C:\SIRI-22\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 664
The same code is working fine when connected to local mysql server.
AS I AM NOT ALLOWED TO POST ANSWER TO MY QUESTION, I AM POSTING SOLUTION TO THIS ISSUE HERE:
I resolved the issue. It is explained in the comments section of azure website (https://learn.microsoft.com/en-us/azure/mysql/howto-configure-ssl).
Explanation :
You might experience problems if you're trying to connect to Azure Database for MySQL over SSL from PHP through MySQLi or PDO. The problem in both cases is that the SSL certificates used by Azure Database for MySQL do not match the hostnames of the servers you're connecting to, and hence server certificate verification fails. (Many other clients will happily connect over SSL without verifying the server certificate, which only makes it harder to figure out what is wrong.)
Fortunately, this can be solved if you're running at least PHP 5.6.16 for MySQLi or PHP v7.1.4 for PDO and you're using the MySQL Native Driver--and Azure App Service meets these requirements. For MySQLi, you will need to add the MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT flag when you connect. For PDO, you will need to set the PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT option to false when you connect.
See https://bugs.php.net/bug.php?id=68344 and https://bugs.php.net/bug.php?id=71003 for details.
This is explained by Warlock. Thanks to him !
Had you check your Azure Firewall connection of your MySQL server, since Microsoft Azure doesn't allow any external connection by default.
You have error in your query builder. You can try
DB::table("users")->get();

PHP pdo remote connection to Oracle error

I have been trying to connect remotely to oracle using pdo
I have enable pdo driver
extension=php_pdo_odbc.dll
But getting this error
"SQLSTATE[HY000]: OCIEnvNlsCreate: Check the character set is valid and that PHP has access to Oracle libraries and NLS data (ext\pdo_oci\oci_driver.c:614)"
Here is my code for connection
try {
$dbh = new PDO("oci:dbname='My server ip'/orcl;charset=UTF-8", "my user name", "my password");
}
catch (PDOException $e)
{ echo $e->getMessage(); }
I had the same errormessage after I installed multiple copies of ODAC/ODT because I didn't know what version I needed to have my Visual Studio projects (Windows Forms app and a ASP.NET website) connect to a Oracle database. While fixing this connection problem in the end, I broke my PHP/PDO website which also needed to connect to the same Oracle database. It gave me the same errormessage as in your question.
I removed all ODAC/ODT client installations and also the Oracle Server to be sure and then I reinstalled only the Oracle Server (in my case Oracle 11.2 Express 64bit) and it worked again. I could connect again from SQL Developer and also through PHP/PDO. After that I installed the correct ODAC/ODT (32bit(!) version because of my 32bit Visual Studio) and now also my Visual Studio Forms project AND ASP.NET website could connect to the Oracle server.

Connect to SQL Server 2008 with PDO

I am trying to connect to SQL Server 2008 (not express) with PHP 5.2.9-2 on Windows XP sp2. I can connect to the remote SQL Server fine from SQL Server Management Studio from the same machine.
My first attempt was this:
$conn = new PDO("mssql:host={$host};dbname={$db}", $user, $pass);
Which gives this error:
PHP Fatal error:
Uncaught exception 'PDOException' with message
'SQLSTATE[0100] Unable to connect: SQL Server is unavailable or does not exist.
Access denied. (severity 9)'
Second attempt (found on experts-exchange)
$conn = new PDO("odbc:Driver={SQL Server Native Client 10.0};Server={$host};Database={$db};Uid={$user};Pwd={$pass}",
$user, $pass);
This works, but I can't use PDO::lastInsertId(), which I would like to be able to:
Driver does not support this function: driver does not support lastInsertId()
Do you have any suggestions? Any help would be greatly appreciated.
i'm pretty sure that pdo with the mssql data server type requires dblib to connect. do you have dblib installed on the machine? make sure you can find ntwdblib.dll somewhere in the path on your system. i know this doesn't jive with the error message you're getting, but i'm not sure i trust the error message.
I updated ntwdblib.dll as suggested here and it now works.
Unfortunately I still can't use PDO::lastInsertId() because apparently this driver does not support it either, so it wasn't really worth the hassle. I can however use the equivalent SELECT ##IDENTITY as Id.
That you cannot use SQL Server authentication because only Windows authentication is permitted.
Check if the server is running Mixed mode authentication.
Also check if this SO question helps you.

Categories