Web Hosting Talk







View Full Version : MySQL database connection tests


HostingInsider
04-04-2005, 10:22 PM
I am working on a PHP/MySQL program and have a question.

It includes a browser-based installation wizard, and on the page after the user inputs their MySQL connection information, I am having it do a check to ensure that the information they inputted was correct. Here is the code I have:

<?php
// This parts define the MySQL database access info to be used in the databse connection test
define ('DBHOST', $_POST[databasehost]);
define ('DBUSER', $_POST[databaseuser]);
define ('DBPASSWORD', $_POST[databasepassword]);
define ('DBNAME', $_POST[databasename]);
// This part attempts to make the connection and displays any error messages
$dbc = @mysql_connect (DBHOST, DBUSER, DBPASSWORD) OR die ('Could not connect to MySQL, please check login details, error message: ' . mysql_error());
@mysql_select_db (DBNAME) OR die ('Could not select the database, error message: ' . mysql_error());
?>

Here's my question: How would I have it display a message if everything checks out fine. I know it would probably be an if statement, but what would I put in the statement? For example, I want it to say:

The test connection to your MySQL database was made successfully!

Could someone please help me out here? Thanks for your input.

RRolfe
04-04-2005, 10:40 PM
wouldn't the following work?


if ($db) {
print("The test connection to your MySQL database was made successfully!");
}

HXM-Jacob
04-04-2005, 10:55 PM
In your code you have the following...


$dbc = @mysql_connect (DBHOST, DBUSER, DBPASSWORD) OR die ('Could not connect to MySQL, please check login details, error message: ' . mysql_error());
@mysql_select_db (DBNAME) OR die ('Could not select the database, error message: ' . mysql_error());


The OR Die() check takes care of the work for you. You can assume that if the script is still processing that everything worked fine, and the connection was successful. die will halt the execution of the script, thus preventing any following code from being executed.

simply place the following after your database connection


echo 'Connection Success!';