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');
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'm trying to access a remote MySQL Database which is only reachable locally (localhost).
My Code looks like this:
$connection = ssh2_connect('IP to Server', 22);
if (ssh2_auth_password($connection, 'User', 'Password')) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');
}
$tunnel = ssh2_tunnel($connection, '127.0.0.1', 3306);
try {
$this->_connection = new PDO($dsn, $config['login'], $config['password'], $flags);
$this->connected = true;
if (! empty($config['settings'])) {
foreach ($config['settings'] as $key => $value) {
$this->_execute("SET $key=$value");
}
}
} catch (PDOException $e) {
throw new MissingConnectionException(array(
'class' => get_class($this),
'message' => $e->getMessage()
));
}
The script is successful till $tunnel. The PDO connection fails with "Connection refused". I think PDO is trying to connect to MAMP? The $dsn variable looks like "mysql:host=127.0.0.1;port=3306;dbname=mydb".
How can I tell MAMP, that 127.0.0.1 is on the remote server connected through SSH?
Thank you!
As you already noted, your script is attempting to connect to your local MySQL server instead of the remote one. In order for PDO to establish a connection with the remote server, you could establish an SSH tunnel and forward a local port to the server port. As shown here https://brettrawlins.com/blog/mysql-ssh-tunnel.
To run the ssh command in php: shell_exec(ssh code here)
You might want to see: Connect to a MySQL server over SSH in PHP
However as noted in there as well, this method is relatively slow as you will have to create a new tunnel each time you query the database, making your queries take quite a bit longer. I suppose for a development environment it's a valid option since you don't have a static IP, but I don't see this working in production.
If you are trying to do this for the sake of encryption, I would recommend connecting to your database with SSL. However note that SSL for PDO might not be supported by all PHP versions.
I'm a beginner and I'm trying to connect remotely to my MySQL database server, but still don't succeed.
I have a dedicated server and my provider doesn't want to help me.
-CentOS 6 with Parallels Plesk 12 (64-bit)-
I'm accessing my server trough Parallels Plesk. I set up a administrator and database user with "allow remote connection from any host".
I'm able to access my server with FTP and the MySQL database server locally, but not from my computer.
I have this error displaying when trying to connect with php:
Warning: PDO::__construct(): MySQL server has gone away in /Users/X/Sites/connexionBDD.php on line 31
Here is the way I connect:
define("DB_SERVER","mywebsite.com:8443");
define("DB_NAME","databasName");
define("DB_USER","MyUser");
define("DB_PWD","MyPassword");
try {
//line 31
$bdd = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PWD);
}
catch (Exception $e) {
die($e->getMessage());
}
echo 'You made it';
For DB_SERVER, if using IP address, do I need to give V4 + port?
The host and port should not be specified in the same variable, split them up and define your DSN like below. Additionally, check the port is the correct one, MySQL by default uses port 3306.
define("DB_SERVER","mywebsite.com");
define("DB_PORT","8443");
define("DB_NAME","databasName");
define("DB_USER","MyUser");
define("DB_PWD","MyPassword");
try {
//line 31
$bdd = new PDO('mysql:host='.DB_SERVER.';port='.DB_PORT.';dbname='.DB_NAME, DB_USER, DB_PWD);
}
catch (Exception $e) {
die($e->getMessage());
}
echo 'You made it';
I created a FB app hosted on Heroku.
Instead of Heroku's database I use a mysql database. I made the chage using this code:
heroku config:add DATABASE_URL=mysql://user:pass#server:port/database_name
So far everything is ok but now I have an issue connecting to the database in my index.php file.
I don't know ho to do.
I tried to connect to the DB as I do on my website
try {
$bdd = new PDO('mysql:host=host;dbname=database_name', 'user', 'pass');
}
catch (Exception $e) {
die('Erreur : ' . $e->getMessage());
}
But I have an error in my app:
Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.
Thank you
Check whether your remote database port is opened for external connection. Look for bind-address configuration of your remote mysql server.
i have site hosted on bluehost.com (Linux Server), i have use case that i have to Export some Specific Data Export to MS Access File,
I have following code to Connect with MS Access File:
$dbh = null;
try{
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$file_path", $user_name, $password, $db_info);
}catch(PDOException $e){
echo $e->getMessage();
}
return $dbh;
But when i run the above code on Hosting server than i got error
SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified
Later after some searching i changed the code of DB connection to
try{
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$file_path", ACCESS_DB_USERNAME, ACCESS_DB_PASSWORD);
}catch(Exception $ex){
echo $ex->getMessage();
}
in above code i did not get any exception but $conn is NULL, both code snippet working fine on my local Windows machine,
Can you people help me in fixing issue?? i want to make connection and want to run INSERT INTO statement on MS Access DB.
The docs for odbc_connect say it returns one of two things.
An ODBC connection.
FALSE on error.
Your code should look for FALSE, not for an exception.
The docs also have examples of several different kinds of connections. But I think the chances that Bluehost installs Microsoft Access drivers on all their Linux servers are less than zero.