PDO Cannot Connect to Remote MySQL - php

I've recently setup a load balancer, it has 3 public servers serving users.
Here's where I have my problem... the site uses PDO to connect to database, the MySQL server is on one single remote server for all them to connect to and it doesn't seem to work.
Here's the error I get:
"Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /var/www/pingrglobe.com/backend/database.php:8 Stack trace: #0 /var/www/pingrglobe.com/backend/database.php(8): PDO->__construct('mysql:host=162....', 'root', 'nope') #1 /var/www/pingrglobe.com/structure/header.php(4): require('/var/www/pingrg...') #2 /var/www/pingrglobe.com/index.php(2): require('/var/www/pingrg...') #3 {main} thrown in /var/www/pingrglobe.com/backend/database.php on line 8"
Here's my code:
$host = "162.243.135.167";
$user = "root";
$pass = "nope;)";
$dbname = "pingrglobe";
$db = new PDO("mysql:host=".$host.";port=3306;dbname=".$dbname, $user, $pass);

Related

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2019] Unknown character set

I am trying to learn about PDOs so I can migrate away from using mysqli functions but I can't seem to get the DSN right when connecting to the database. I don't know what charset to use. Is it something that I determine in phpmyadmin or is it something that is already set and I have to find? Not sure.
I have a MySQL database set up on a XAMPP program that I am trying to connect to. Below is my code from my db.php file and the full error message.
// CREATE PDO OBJECT WITH CONSTRUCTOR
$db = new PDO('mysql:host=localhost;dbname=phpdemo;charset=utf8', 'root', '');
// SET ATTRIBUTE RELATED TO ERROR HANDLING FOR SECURITY
$db -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$fb -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2019] Unknown character set in C:\xampp\htdocs\PHP\PDO Lab\includes\db.php:3 Stack trace: #0 C:\xampp\htdocs\PHP\PDO Lab\includes\db.php(3): PDO->__construct('mysql:host=loca...', 'root', '') #1 C:\xampp\htdocs\PHP\PDO Lab\includes\header.php(6): include_once('C:\xampp\htdocs...') #2 C:\xampp\htdocs\PHP\PDO Lab\new_user.php(2): include_once('C:\xampp\htdocs...') #3 {main} thrown in C:\xampp\htdocs\PHP\PDO Lab\includes\db.php on line 3
Edit: I have tried using utf8mb4 which results in the same error.
Edit2: I just realized that I don't get any errors when I open my db.php file. I do get errors when I open my new_user.php file who's only code is "include('db.php');"

Fatal error: Uncaught exception 'MongoConnectionException' with message

I am testing the mongoDB in localhost and successfully installed the mongoDB in localhost. I have tried to connect the mongoDB using PHP in localhost.
It throws the error like 'Authentication failed'. It is successfully authenticated in cmd prompt.
Below is my code:
$username = 'testUser';
$password = 'testPassword';
$m = new MongoClient("mongodb://testUser:testPassword#localhost", array("db" => "testdb"));
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017: No connection could be made because the target machine actively refused it. ' in E:\xampp\htdocs\xampp\test.php:3 Stack trace: #0 E:\xampp\htdocs\xampp\test.php(3): MongoClient->__construct('localhost:27017', Array) #1 {main} thrown in E:\xampp\htdocs\xampp\test.php on line 3
I have also changed the localhost to ip(127.0.0.1) address but no use. Thanks in advance

PHP PDO connection to remote server connects to local server instead

I am wondering when I instantiate a new PDO object it still uses the local IP of the server.
<?php
$dsn = 'mysql:host=10.0.0.4;charset=utf8';
$username = 'user';
$password = 'pass';
$pdo = new PDO($dsn, $username, $password);
?>
It gives me this weird error.
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[28000] [1045] Access denied for user 'user'#'10.0.0.5'
(using password: YES)' in /var/www/html/mysql_test.php:7
Stack trace:
#0 /var/www/html/mysql_test.php(7): PDO->__construct('mysql:host=10.0...', 'user', 'pass')
#1 {main}
thrown in /var/www/html/mysql_test.php on line 7
I noticed that it is connecting to 10.0.0.5 which is the IP address of the local server. The remote server (10.0.0.4) enables remote access.
Is there an internal config file for PDO that I should configure? The current PHP and PDO versions are 5.5.15RC1 and 5.5.38 respectively.

Error when using PDO

So I just finally installed the PDO drivers, and now I have another issue. When I try to use
<?php
$username = "root";
$password = "allen123";
$conn = new PDO('mysql:host=localhost;dbname=test', $username, $password);
?>
Just to connect to the DB, I get the following error
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY000] [2002] No such file or directory' in
/Users/idrisk/Colourity/site/index.php:4 Stack trace: #0
/Users/idrisk/Colourity/site/index.php(4):
PDO->__construct('mysql:host=loca...', 'root', 'allen123') #1 {main}
thrown in /Users/idrisk/Colourity/site/index.php on line 4
I've looked at other posts on SO about this issue, but they all seem different. Any ideas?
EDIT
So I installed the drivers via Macports and this is what I get once It was installed
To use mysqlnd with a local MySQL server, edit /opt/local/etc/php55/php.ini and set mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket to the path
to your MySQL server's socket file.
For mysql51, use /opt/local/var/run/mysql51/mysqld.sock
For mysql55, use /opt/local/var/run/mysql55/mysqld.sock
For mysql56, use /opt/local/var/run/mysql56/mysqld.sock
For mariadb, use /opt/local/var/run/mariadb/mysqld.sock
For percona, use /opt/local/var/run/percona/mysqld.sock`
The other answers say to use something else.

Connecting to SQL Server with PDO_MSSQL in PHP

I have a problem while trying to connect to my SQL Server 2000 database using pdo_mssql with this line:
$connection = new PDO ('mssql:host=localhost;dbname=DNAME', 'user', 'password');
The exception is the following:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] General SQL Server error: Check messages from the SQL Server. (severity 5)' in C:\Inetpub\wwwroot\public\test.php:2 Stack trace: #0 C:\Inetpub\wwwroot\public\test.php(2): PDO->__construct('mssql:host=loca...', 'user', 'password') #1 {main} thrown in C:\Inetpub\wwwroot\public\test.php on line 2
If I make the connection using the function mssql_connect() as follows, I obtain no error and my connection is correctly made:
$link = mssql_connect ('localhost', 'user', 'password');
Any idea? Where can I read the detailed error message? Thanks
My database name had the character "-", removing it solved the problem!

Categories