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());
}
?>
Related
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 am trying to connect to a remote mysql server using PDO php script. Script is working fine when run from the terminal.I am able to connect to the remote mysql database.
function db_connect($host, $username, $password, $db_name){
global $conn;
try{
$conn = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
$conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "success";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
}
But when I run this php script from browser, I got following error-
ERROR:SQLSTATE[HY000] [2003] Can't connect to MySQL server
First I though it is because I am running the script from root user.
But I tested it with a test user and it still runs from terminal but not from the browser.
I am also able to connect to the database from mysql workbench.
I don't know the cause. Please suggest some solution.
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'm using PDO's as PHP extension. My project works fine in local server, but when I upload it on a live server... it throws a connection error..
Here goes my connection.php file
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=cms','root','');
}
catch(PDOException $e) {
exit('Database Error');
}
?>
Check if the MySQL database ist hosted on the same server as the script is. Otherwise you cannot use localhost.
In addition you may add a password for the root user, which you should have configured already.
$pdo = new PDO('mysql:host=localhost;dbname=cms', 'root', 'yourpwhere');
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.