Jump to content

Testing database connection


PRodgers4284

Recommended Posts

I am trying to test a database connection to a mySql database, i want to output a message if the connection to the database is successfully connected, if not output an error. Anyone help with this?

 

The connection im using is:

 

<?php

/**
* Connect to the mysql database.
*/
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db('localdatabase', $conn) or die(mysql_error());

?>

Link to comment
https://forums.phpfreaks.com/topic/101684-testing-database-connection/
Share on other sites

<?php
$conn = mysql_connect("localhost", "root", "");
if ($conn)
{
echo "Successfully connected.<br />";
}
else
{
die(mysql_error())
}

if (mysql_select_db('localdatabase', $conn))
{
echo "Successfully selected a database<br />";
}
else
{
die(mysql_error());
}
?>

 

Try that, it should echo a message on success and display the error message on failure.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.