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');
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'm using PDO for connect my DB with my form HTML for create advanced registration.
The problem is that I receive this error message:
Erreur: SQLSTATE[HY000] [1044] Access denied for user 'mysuser'#'%' to database 'advanced-registration'
I check the login info, everything is ok.
The database is hosted by OVH.
My code for connection:
try {
$db = new PDO('mysql:host=myhost; dbname=advanced-registration','myuser', 'mypass');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db = null;
} catch (PDOException $e) {
echo "Erreur: ".$e->getMessage()."<br/>";
die();
}
I have a fatal error showing when I am trying to get MySQL to use a PDO instance in Godaddy shared hosting.
Code I tried to connect my database:
code 1
try{
$connect = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'user', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
die('Error connecting to database');
}
When I used this code, it is printing 'Error connecting to database'
code 2
$dsn = 'mysql:host=localhost;dbname=dbname';
$user = 'user';
$password = 'password';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
When I used this code it is printing an error:
'Connection failed: SQLSTATE[HY000] [1044] Access denied for user 'user'#'localhost' to database 'dbname"
Godaddy support is saying it is a string error that I have to review from my end and they are asking paid service to resolve it from their end. I found this both code running in my local server (xaamp).
First, make sure everything is correct in your config array, then add '\' this before PDO instance:
// Define Config Options
$config = [
'username' => 'YourUsername',
'password' => 'YourPassword',
'database' => 'YourDatabase'
];
// Make a function to easily access it
function connect($config)
{
try{
$conn = new \PDO('mysql:host=localhost;dbname=' .$config['database'], $config['username'], $config['password']);
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $conn;
}
catch(Exception $e){
return false;
}
}
In shared hosting you normally find that the mysql server is not localhost, you normally find that your provider will place the mysql server on a separate machine.
Login in to you control panel with godaddy and look at the settings, you'll probably have to create a database first.
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
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();
}