Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'dbuser'#'localhost' (using password: YES) - pdo

Most answers were that it would be a right issue. But I assume not in this case, because mysql-connection still worked.
I am trying to connect to a mariadb10, which was on a server in my network
I am trying to connect from my localhost.
If I try in my terminal:
myuser#mylocalComputer ~ $ mysql -h myserver -P 3307 -u mydbuser -pmyconfidentalpassword
everything works fine!!
But If I try to connect by php scrypt by pdo I get the error:
<?php
$dsn = 'mysql: host=myserver:3307;dbname=mydbname';
$username = 'mydbuser';
$password = 'myconfidentalpassword';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
if(! $dbh ) {
die('Could not connect: ' . mysql_error());
}
$sql = 'select * from foo;';
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not select data: ' . mysql_error());
}
echo "Synced data successfully\n";
mysql_close($conn);
?>
The Error was:
PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access
denied for user 'mydbuser'#'localhost' (using password: YES) in
/home/myuser/Projekte/Hibiscus_extend/Hib_Nightly_sync.php:8
Stack trace:
/home/myuser/Projekte/Hibiscus_extend/Hib_Nightly_sync.php(8):
PDO->__construct('mysql: host=myse...', 'mydbuser',
'myconfidentalpassword', Array) {main} thrown in
/home/myuser/Projekte/Hibiscus_extend/Hib_Nightly_sync.php on line 8
What did I do wrong, and where?

Oh Oh Oh!
I found the solution!
The reason for the connection problem were just the empty spaces in $dsn
Wrong:
$dsn = 'mysql: host=myserver:3307;dbname=mydbname';
works fine
$dsn = 'mysql:host=myserver:3307;dbname=mydbname';

Related

How to turn off PDO error message

I am trying to use phpunit to test connection, I already make error_reporting(0) and assign PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT, but when I give the wrong information on purpose, it always dump error message, for example, if I give the wrong account, it shows
PDOException: SQLSTATE[HY000] [1045] Access denied for user 'test'#'localhost' (us
ing password: YES)
How to turn it off?
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => false,
);
$dsn = 'mysql:host=' . $config['host'] . ';dbname=' . $config['database'] . ';charset=utf8';
try {
$this->dbh = new PDO($dsn, $config['username'], $config['password'], $options);
} catch (PDOExeception $e) {
// Do something
}
PDO::__construct will always throw a PDOException if the connection fails. There is nothing you can do about that. Any sane application should throw exception if it fails to connect or maybe you have good reason to explain why you need to turn off exception on connections.
Beside the connection It will work on others transactions.
Example:
<?php
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
PDO::ATTR_PERSISTENT => false,
);
$dsn = 'mysql:host=' . $config['host'] . ';dbname=' . $config['database'] . ';charset=utf8';
try {
//THIS WILL THROW EXECPTION IF FAILED, NO MATTER WHAT ERROR MODE YOU SET
$this->dbh = new PDO($dsn, $config['username'], $config['password'], $options);
} catch (PDOException $e) {
// Do something
}
//JUST SILENT THIS WILL NOT CAUSE EXCEPTION
$dbh->query("SELECT badColumn FROM wrongTable");
?>

SQL error connecting to database - WAMP

I have WAMP installed. I am trying to connect the database and it will not work.
I have all services started... I created a test file calledtes44.php and put it in the WWW folder to see if it wpuld work
The code is
<?php phpinfo() ; ? >
When I run that file in browser I receive the error...
Parse error: syntax error, unexpected '?' in C:\wamp1\www\test44.php
on line 1
What am I missing??
James
You have a space at the end tag.
Try it like this:
<?php phpinfo(); ?>
You are using the wrong quotes.
Use this:
$conn = mysqli_connect("localhost", "", "", "membersapp") or die(mysqli_error($conn));
Instead of this:
$conn = mysqli_connect(“localhost”,””,””,”membersapp”) or die(mysqli_error($conn));
Also, add a semicolon at the end of your echo:
echo "Connected to database";
This should be it:
// The connection string
$con = mysqli_connect("localhost", "", "", "membersapp");
// Check connection
if (mysqli_connect_errno())
{
// Error!
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
// Woohoo, success!
echo "Connected succesfully!";
}

WP error connecting to DB

I get this message when I try to access my site.
The site was working properly, and I tried to add google analytics...
Anyway, sodabrasil was a site created out of WP.
sodabrasil.com/paskin was WP.
We did the integration of logins. Everything was working.
When I run http://sodabrasil.com/dbtest.php, it says it's connected.
this is the debug code.
<?php
//connect to MySQL Server
$conn = mysql_connect("localhost", "*****", "******");
if (!$conn)
{
die(mysql_error());
}
else
{
echo "1. Successfully connected to MySQL database server.<br /><br />";
}
// select database
$db_sel = mysql_select_db('*********', $conn);
if (!$db_sel)
{
die ('cant\'t use database: ' . mysql_error());
}
else
{
echo "2. WordPress database selected. <br />";
}
?>
the database * is the DB sodabrasil uses for the login.
But accessing sodabrasil is blocked even before the login!!!
I am not a professional coder, which makes things a lot harder.
This is the real config file:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
define('WP_USE_THEMES', false);
//echo $_SERVER['DOCUMENT_ROOT'];
//die;
//require_once('./blog/wp-blog-header.php');
require_once('/home/houswe5/public_html/sodabrasil.com/paskin/wp-blog-header.php');
global $mysqli;
$server = 'localhost'; // The host you want to connect to.
$username = '****'; // The database username.
$password = '*****'; // The database password.
$database = '*******'; // The database name.
$dbms = 'MySQL 5';
$dbport = '3306';
//$dbh = mysql_connect($server, $username, $password) or die("Unable to connect to SQL server");
//$my_db = #mysql_select_db($database) or die("Unable to select database");
$mysqli = new mysqli($server, $username, $password, $database);
//$mysqli = mysqli_connect($server, $username, $password, $database) or die("Error " . mysqli_error($link));
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;die;
}
// If you are connecting via TCP/IP rather than a UNIX socket remember to add the port number as a parameter.
?>
Exact same credentials.
But this url (http://sodabrasil.com/db_connect.php) throws the error message in regards to DB.
Thank you for you help.
G.

MongoClient, connect with '#' in password

I've been trying to experiment with MongoDb, and when I run the script to connect, it can't because the password uses the # symbol.
To connect to MongoDb
mongodb://username:password#localhost
But as my password to connect to MongoDb uses the # symbol, it can't connect due to the # symbol.
For example;
new MongoClient("mongodb://mongo:p#assword#localhost);
Will throw the following error;
PHP Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: assword!#localhost:27017: Couldn't get host info for assword!#localhost'
As you can see, it thinks the first # in the password is the seperator for password and host.
Is there a way to connect, and allow the # symbol in the password when using MongoClient?
http://uk3.php.net/manual/en/mongoclient.construct.php you can pass the username and password as an options array
new MongoClient("mongodb://localhost:27017", array("username" => "joe", "password" => "test"));
You can try by this code. Just pass your input as per dynamic value.
try {
$m = new MongoClient("mongodb://HOST:" . MNG_PORT, array(
"username" => MNG_USER,
"password" => MNG_PWD
)); // connect local Mongo
$db = $m->selectDB("DBNAME"); // select DB
$collection = $db->collections; // connect to collection
} catch ( MongoConnectionException $e ) {
echo 'Couldn\'t connect to mongodb, is the "mongo" process running?';
exit();
}

Apple Push Notification from Server

I have followed :
Apple Push Notification Services Tutorial.
And it worked for me locally.
Next, i want to send push notifications from my server?
I have uploaded simplepush.php and ck.pem to my server. When i check http://www.myserver/simplepush.php it gives me error:
*Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195
(Connection timed out) in /home/cherry/public_html/simplepush.php on
line 21 Failed to connect: 110 Connection timed out*
Could you please help me?
PHP Code:
<?php
// Put your device token here (without spaces):
$deviceToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// Put your private key's passphrase here:
$passphrase = 'xxxxxxxxxx';
// Put your alert message here:
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
Is the PHP compiled with SSL? And is it the same version as your local version?
Or maybe the firewall of some kind blocks connection to 2195 port from your server?
Can you log in to this server (to a shell of some kind) and check if you can connect via telnet to this server and port:
$ telnet gateway.sandbox.push.apple.com 2195