How can I catch and ignore errors when connecting to a database and display a simple text message instead?
My connection looks like this:
$connection = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
If the connection fails I want it simply to echo "Error connecting to the database" instead of the big error box.
EDIT:
Although error_reporting(0) did work, what worked better was just putting '#' in front of mysqli_connect:
$connection = #mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
if (!$connection) {
$connection_status = 'Connection to database failed';
} else {
$connection_status = null;
}
?>
<?php echo $connection_status; ?>
Taken from the documentation of mysqli_connect:
Examples
Example #1 mysqli_connect() example
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
mysqli_connect:
$connection = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
if (!$connection)
{
error_reporting(0);
die("Error: Unable to connect to MySQL." . PHP_EOL);
}
To disable "big error boxes" (I assume standart messages?), write error_reporting(0); above this.
You may set your custom handler for errors
register_shutdown_function("shutdownHandler");
function shutdownHandler()
{
if (!is_null($e = error_get_last())).
{
echo $e['message'];
}
}
Related
I have Apache (with PHP) and MySQL installed and running on my Raspberry Pi. I've done some simple tests with PHP, and it seems to be working. And MySQL is working perfectly from the terminal, and even another computer.
I made this PHP file:
<?php
echo("Connecting");
$connection = new mysqli("127.0.0.1", "admin", "password", "test");
if ($connection->connect_error) {
die("Connection error");
}
echo("Connection successful");
?>
Yet when I go to this page in my web browser, all I see is "Connecting". If I comment out the connection command, I see "Connecting Connection successful" in the browser.
It seems as if the PHP code stops running or hangs at the connection command.
Any ideas why I'm having this strange behavior?
Try adding these lines at the top: error_reporting(E_ALL);
ini_set('display_errors',1);
I hope You should do following
if ($connection->connect_error) {
echo "Connection error";
}else{
echo "Connection successful";
}
Try this instead
<?php
$link = mysqli_connect("127.0.0.1", "admin", "password", "test");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
I am trying to get my PHP pages to connection to a remote server hosting the MySQL database. I can connect via the command line just fine with the same username and password. Below is a simple test file I created. The only thing I've done to the code is remove the password, but I know that's not the issue. Like I said I can connect via the CLI. This is a Linux install. When viewed from the web browser, all I get is an error 500 page. If I comment out the mysqli statement, the page displays the Connected successfully.
<?php
$servername = "12.0.1.170";
$username = "jason";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
/ die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Per the php documentation for mysqli_connect()
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
Your code appears to be lacking all four parameters required.
You have:
$conn = mysqli_connect($servername, $username, $password);
Perhaps try:
$conn = mysqli_connect($servername, $username, $password, $database);
with a $database='db_name'; value added into your variables section.
There are two possible issues with remote mysql connection:
1. Firewall of the server: You must enable incoming connection on port (for. e.g. 3306).
2. User in mysql: You must have a user created (for e.g. jason in this case)
Try connecting this way
$con = mysqli_connect('localhost','root','','db_name');
if($con)
echo "hell yeah its connected";
else
echo "i m boned"
I am looking to see where I place the mysqli replacement code so that I can eliminate the connect errors I get...
function DBLogin()
{
$this->connection = mysql_connect($this->db_host,$this->username,$this->pwd);
if(!$this->connection)
{
$this->HandleDBError("Database Login failed! Please make sure that the DB login credentials provided are correct");
return false;
}
if(!mysql_select_db($this->database, $this->connection))
{
$this->HandleDBError('Failed to select database: '.$this->database.' Please make sure that the database name provided is correct');
return false;
}
if(!mysql_query("SET NAMES 'UTF8'",$this->connection))
{
$this->HandleDBError('Error setting utf8 encoding');
return false;
}
return true;
}
I have the following example, just not sure what I have to remove from the code above to get it to work. Any help?
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
I'm trying to connect to a database via MySQL but it does not work. I'm getting the following error:
"Connection failed: No connection could be made because the target machine actively refused it. "
Here's the basic code:
<?php
$servername = "server";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
I can connect to the database from Excel and also via an ODBC connection with the code below but not via MySQL:
<?php
$user = 'username';
$pass = 'password';
$server = 'server';
$database = 'database';
// No changes needed from now on
$connection_string = "DRIVER={SQL Server};SERVER=$server; DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if (!$conn){
exit("Connection to the database Failed: " . $conn);
}
echo "Connected successfully";
?>
Any ideas?
You say you can connect with odbc which means you have an SQL Server database, not a Mysql database. If you don't like odbc, pdo is your other option. Mysqli is not an option as its mysql only.
Try this code:
<?php
$link = mysqli_connect('server','username','password','database');
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo("success");
?>
I am having trouble connecting to my localhost database with php. It feels like I have followed every tutorial there is.
current php code:
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="webutvshop";
$username="dbconnect";
$password="password";
//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
die ('Can\'t select database: ' . mysql_error());
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysql_close($link);
?>
The issue stands, when I acess the file locally on my computer I do not get any answer at all from it. I've tried with many other, yet I do not get any answers from them!
I need help in order to keep working on my schoolproject, thanks.
Stop using mysql_*, because they are deprecated officially. Use mysqli_* or PDO for this purpose. An example:-
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="stackquestion";
$username="root";
$password=""; // check once with empty password and once with some password that you tried already
//DO NOT EDIT BELOW THIS LINE
$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysqli_select_db($link,$database);
if (!$db_selected) {
die ('Can\'t select database: ' . mysqli_error($link));
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysqli_close($link);
?>
Output:- http://prntscr.com/7cbr5j
Can you also verify you can connect to the database from a command line:
mysql -u dbconnect -ppassword -h localhost webutvshop