I have the following code:
try {
$db_conn = new PDO('mysql:host='.$host.';dbname=stats;port='.$port, $un, $pw);
} catch (PDOException $e) {
WriteLog("Could not connect to database!\nError: ".$e->getMessage());
exit;
}
try {
$db_conn2 = new PDO('mysql:host=localhost;dbname=log', $un2, $pw2);
} catch (PDOException $e) {
WriteLog("Could not connect to database[2]!\nError: ".$e->getMessage());
exit;
}
It connects without errors to the first server (not local), but then it fails to connect to the local server. I get this error message:
Error: SQLSTATE[HY000] [2002] No such file or directory (||)
I'm running PHP v5.4.27
Solved it. Changing from localhost to 127.0.0.1 seems to fix it. I'm not sure why
Related
This is view of my phpmyadmin web server
I need to connect to my university webserver on PHPmyadmin which is "phpmyadmin.newnumyspace.co.uk"
what changes are required to achieve this. I mean i know localhost would be changed and port and something needs to be put here but help me out as i don't understand the syntax
<?php
$db_host="localhost";
$db_user="root";
$db_password="";
$db_name="nbl";
try{
$db=new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOEXCEPTION $e)
{
$e->getMessage();
}
?>
Your sql DB is local to the web host as they are both run on a linux XAMPP server. Obviously include the correct username and password.
function getConnection() {
try {
/* connects to the SQL DB */
$connection = new
PDO("mysql:host=localhost;dbname=unn_w19038752",
"unn_w19038752", "Password");
$connection->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
return $connection;
} catch (Exception $e) {
/* displays an error if unable to connect */
throw new Exception("Connection error ". $e->getMessage(), 0, $e);
}
}
Then you can just call this function everytime you need to make a connection
($dbConn = getConnection();)
Although for a clearer answer you're better speaking to your KF4009 Module tutor for assistance (That's what you're paying Uni fees for :-) )
Regards, former NU Student :-)
P.S. nbl is a table not a db, and you don't have root access(username), check your email from newnumyspace for your correct server login details.
I am reading some extra information from Redis and the desired behaviour is to skip connection error silently, if any:
try {
$r = new Redis();
$r->connect("127.0.0.1", "6379");
} catch (Error $e) {
;
} catch (Throwable $e) {
;
}
If Redis fails, monitoring system will show alert to right people to fix it.
Unfortunatelly the code above still causes Yii to fail and produce HTTP 500:
2018/04/09 12:28:04 [error] [php] Redis::connect(): connect() failed: Connection refused
What am I doing wrong?
You need to catch the Exception thrown...
try {
$r = new Redis();
$r->connect("127.0.0.1", "6379");
} catch (\Exception $e) {
;
}
I think you can catch the very specific exception of Predis\Connection\ConnectionException if you need to.
I have written a function which checks if rabbitmq is running.
function getBrokerStatus()
{
log_message("info", "Checking if broker is running....");
try {
$amqpConnection = new AMQPConnection();
$amqpConnection->setLogin("guest");
$amqpConnection->setPassword("guest");
$amqpConnection->setVhost("/");
$amqpConnection->connect();
} catch (Exception $e) {
log_message("info", "Exception: " . $e->getMessage());
return false;
}
if (!$amqpConnection->isConnected()) {
log_message("info", "Cannot connect to the broker! It might not be running");
return false;
}
$amqpConnection->disconnect();
return true;
}
My code catches this exception. I see below in logs -
Exception: Socket error: could not connect to host.
But my rabbitmq server is running then why am I getting this exception? I am using v3.1.1 of rabbitmq-server.
if you cannot ping your localhost on your machine, it must be you didn't setup your hosts file.
Edit it in /etc/hosts
Okay, so this is my code:
try {
$pdo = new PDO ('mysql:host=127.0.0.1;dbname=db_name','user','password');
} catch (PDOException $e) {
exit ('Database error.');
}
I tried so many different combinations with host name, username and password, and every time I get 'Database error'.
My question is:
What should I do to succesfully transfer MySQL database from localhost to my hosted server using this part of code (which is my connection.php file)?
Thank you, in advance.
Get rid of this try catch stuff. Leave it as
$pdo = new PDO ('mysql:host=127.0.0.1;dbname=db_name','user','password');
And see what it says (on-screen or logs)
Use this try and catch block for error trace
try {
$this->conn = new PDO('mysql:host=127.0.0.1;dbname=db_name','user','password');
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
I have a website with a database connection. The connection is very important for the website, so I need to 'die' the website if it can't establish a database connection. The 'or die' isn't a good idea, so I tried this:
$host='localhost';
$un='root';
$pw='mypass';
$dbname='home';
try {
$db = new mysqli($host, $un, $pw, $dbname);
if ($db->connect_errno) {
throw new Exception('Fail: '.$db->connect_errno);
}
}
catch(Exception $e)
{
die($e->getMessage());
}
But then, the user can see the error message:
mysqli::mysqli() [mysqli.mysqli]: php_network_getaddresses: getaddrinfo failed: no such host is known
Fail: 2002
What is the best way to check if the connection is ok, and if not: die the website with an error message?
Thanks for the help!
catch(Exception $e)
{
die($e->getMessage());
}
Will print a message and terminate the script
Log the error and die with custom message
error_log($e->getMessage());
die('custom message');