Simple Php Mysql Connection Test Script Example
Posted On Tue Feb 05, 2008 By hostman In MySQL Forums And Topics Discussions About Open Source Database Forums
as a php programmer, sometimes you need to create scripts with an install for the installation of the script. and you want to test the mysql database connection before you install it. today i was writing a script and i wanted to take the user through the installation process and one of the steps was to test the mysql database connections, if the connections fails, then the user is prompted to enter the database details again, if its successful, then it continues with the next step in the instalation process. so you can use this:
you have two options:
OPTION 1 - IF you dont want to create the files yourself, Webune has provided the full script, you can download the .zip file by going to this post: How To Test Mysql Database Connection
OPTION 2 - You can follow the steps below to create the script yourself:
1. first step is to copy and paste the following code into your texteditor. if you are using windows, you can use notepad.
PHPCODE: [ testmysql.php ]
2. save the file as testmysql.php and upload to your website. NOTE: you must have php and mysql for this sample script to work
3. now open testmysql.php with your browser ( http://www.example.com/testmysql.php ), you will see if it fails or if it is able to connect successfuly
hope this helps
UPDATE: 12/19/2012 Thanks for your comments. I see that some of you have had trouble with the code. I have tested it again and it works ok on my apache server using XAMPP on my windows 7 laptop. So I created a short video to show you as proof that it works.
you have two options:
OPTION 1 - IF you dont want to create the files yourself, Webune has provided the full script, you can download the .zip file by going to this post: How To Test Mysql Database Connection
OPTION 2 - You can follow the steps below to create the script yourself:
1. first step is to copy and paste the following code into your texteditor. if you are using windows, you can use notepad.
PHPCODE: [ testmysql.php ]
<?php
ini_set('display_errors', 1); // DISPLAY ANY ERRORRS ON SCRIPT
###################################### C O P Y R I G H T S ####################################
# THIS SCRIPT IS DISTRIBUTED BY WEBUNE.COM UNDER LICENSE UNDER THE GPL RULES
# PLEASE DO NOT REMOVE THIS MESSAGE IN SUPPORT OF OUR HARD WORK TO CONTINUE TO PROVIDE FREE SUPPORT
###############################################################################################
# OK, HERE WE GO
# Use this varialble if you are using an installation script
$step = $_GET['step'];
if (!$step) {
$page_title = 'Form';
}
else{
$page_title = 'Test MySQL step '.$step;
}
############## BEGIN FUNCTIONS ##############################
# FUNCTION TO TEST USERNAME AND PASSWORD IN MYSQL HOST
function db_connect($server, $username, $password, $link = 'db_link') {
global $$link, $db_error;
$db_error = false;
if (!$server) {
$db_error = 'No Server selected.';
return false;
}
$$link = @mysql_connect($server, $username, $password) or $db_error = mysql_error();
return $$link;
}
# FUNCTION TO SELECT DATABASE ACCESS
function db_select_db($database) {
echo mysql_error();
return mysql_select_db($database);
}
# FUNCTION TO TEST DATABASE ACCESS
function db_test_create_db_permission($database) {
global $db_error;
$db_created = false;
$db_error = false;
if (!$database) {
$db_error = 'No Database selected.';
return false;
}
if ($db_error) {
return false;
} else {
if (!@db_select_db($database)) {
$db_error = mysql_error();
return false;
}else {
return true;
}
return true;
}
}
function step1 ($error) {
echo '<h1 style="color:#FF0000">'.$error.'</h1><hr>';
?>
<form name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>?step=2">
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<td><div align="right">mysql hostname:</div></td>
<td><input name="server" type="text" value="<?php echo $_REQUEST['server']; ?>">
(usually "localhost" or enter IP Address of MySQL Server)</td>
</tr>
<tr>
<td><div align="right">mysql username:</div></td>
<td><input type="text" name="username" value="<?php echo $_REQUEST['username']; ?>"></td>
</tr>
<tr>
<td><div align="right">mysql username password:</div></td>
<td><input type="text" name="password" value="<?php echo $_REQUEST['password']; ?>"></td>
</tr>
<tr>
<td><div align="right">mysql database name:</div></td>
<td><input type="text" name="database" value="<?php echo $_REQUEST['database']; ?>"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
<?php
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Webune MYSQL TEST - <?php echo $page_title; ?></title>
</head>
<body>
<h1><?php echo $page_title; ?></h1>
<?php
############## END FUNCTIONS ##############################
switch ($step) {
case '2':
if ($_REQUEST['server']) {
$db = array();
$db['DB_SERVER'] = trim(stripslashes($_REQUEST['server']));
$db['DB_SERVER_USERNAME'] = trim(stripslashes($_REQUEST['username']));
$db['DB_SERVER_PASSWORD'] = trim(stripslashes($_REQUEST['password']));
$db['DB_DATABASE'] = trim(stripslashes($_REQUEST['database']));
$db_error = false;
db_connect($db['DB_SERVER'], $db['DB_SERVER_USERNAME'], $db['DB_SERVER_PASSWORD']);
if ($db_error == false) {
if (!db_test_create_db_permission($db['DB_DATABASE'])) {
$error = $db_error;
}
} else {
$error = $db_error;
}
if ($db_error != false) {
$error = "failed";
echo step1($db_error);
} else {
echo '<h1 style="color:green">Congratulations!</h1>
Connected Successfuly to datbase <strong>
<a href="http://www.webune.com/forums/how-to-test-mysql-database-connection.html">Continue >> </a></strong>';
}
} else {
$error = "ERROR: please provide a hostanme";
echo step1($error);
}
break;
default:
echo step1('Step 1');
break;
}
?>
<div align="center">
<img src="http://www.webune.com/images/headers/default_logo.jpg">
</div>
<div align="center">
Script Courtesy of
<a href="http://www.webune.com">Webune PHP/Mysql Hosting</a></div>
</body>
</html>
2. save the file as testmysql.php and upload to your website. NOTE: you must have php and mysql for this sample script to work
3. now open testmysql.php with your browser ( http://www.example.com/testmysql.php ), you will see if it fails or if it is able to connect successfuly
hope this helps
UPDATE: 12/19/2012 Thanks for your comments. I see that some of you have had trouble with the code. I have tested it again and it works ok on my apache server using XAMPP on my windows 7 laptop. So I created a short video to show you as proof that it works.
msuding Wed Mar 21, 2012
There are 2 small non-critical spelling mistakes in the code on line 115
"Connected Successfuly to datbase"
second "l" needed in "successfully" and "a" missing in "database"
"Connected Successfuly to datbase"
second "l" needed in "successfully" and "a" missing in "database"
hostman Wed Dec 19, 2012
Thanks, Sorry about my bad spelling. I have Corrected
From:
Connected Successfuly to datbase
To:Connected Successfully to database
From:
Connected Successfuly to datbase
To:Connected Successfully to database
Scott Tue Feb 07, 2012
Great post. Here’s a tool that lets you build your online database in minutes, without coding casp
Max Wed Jan 25, 2012
excellent. work like charm.
binaryman Sat Oct 08, 2011
Thank you, I have been trying to figure this out for a bit while manually setting up Apache, php, MySQL and now moving on to phpMyAdmin
does anyone have a good newbie reference for a home web-server phpMyAdmin guide?
does anyone have a good newbie reference for a home web-server phpMyAdmin guide?
Nixon Sat Oct 01, 2011
Very usefull! Thank you!
anonymous Sun Sep 04, 2011
no so simple...
Colin Tue May 31, 2011
Very handy - it helped me a lot, thanks!
Mon May 16, 2011
Oh great tutorial!!! It’s help me a lot.
Also php-example helps me to learn PHP, MySQL & CakePHP with ready made code examples.
Kelly
Web Developer, University of Technology, Sydney, Australia
Also php-example helps me to learn PHP, MySQL & CakePHP with ready made code examples.
Kelly
Web Developer, University of Technology, Sydney, Australia
wallpaperama Sun Feb 27, 2011
you probably are getting an error, but you may have display_errors = Off
make sure you can display php errors, otherwise, you will never know if your scripts fails or not
make sure you can display php errors, otherwise, you will never know if your scripts fails or not
James Sun Feb 27, 2011
Ok, I've run the script, and I get to the second page.
It says "Test MySQL step 2" and nothing else.
I imagine it's supposed to say something like Congrats you've connected! or something like that.
How do I go about rectifying this situation?
It says "Test MySQL step 2" and nothing else.
I imagine it's supposed to say something like Congrats you've connected! or something like that.
How do I go about rectifying this situation?
Bala Mon Nov 08, 2010
Hi its working properly...
Nice article....
Its very useful who are learning PHP Basic...
Please post like this type of article...
Nice article....
Its very useful who are learning PHP Basic...
Please post like this type of article...
Thom Thu Sep 30, 2010
I get step 2 message not congratulations, how can I fix this?
PHP not working with mysql but mysql works and other php scripts work.
PHP not working with mysql but mysql works and other php scripts work.
victor Sun Aug 08, 2010
smart a good script to study and easy to get, kp it up,cheerz!
mihajlo Wed Apr 28, 2010
tnx 4 the code/script
i hope it will be some help
i hope it will be some help
bobv Tue Dec 22, 2009
it's cool! i was looking for sqlite but what i saw is mysql. i was getting impatient with my problem with mysql so i wss trying to migrate to sqlite. anyway, my connection to mysql according to this website is okay. so, i figured out why i cannot connect. so what i did was changed "localhost" to "127.0.0.1" and it worked! what's the difference between the two?
Related Content
Information
Forums »
MySQL Forums And Topics Discussions About Open Source Database »
Simple Php Mysql Connection Test Script Example
MySQL Forums And Topics Discussions About Open Source Database »
Simple Php Mysql Connection Test Script Example
Title: Simple Php Mysql Connection Test Script Example
Description: this page shows you how you can test your MySQL database using a simple script using PHP and HTML. also shows a video with step by step
Tags: simple ,php ,mysql ,connection ,test ,script ,example
Info: This Post Has Been Viewed 0 Times Since
Date: Tue Feb 05, 2008
Author hostman Received 29 Replies #886
Date: Tue Feb 05, 2008
Author hostman Received 29 Replies #886
Share
URL: 

Embed: 

To embed this topic, just copy the code from the "Embed" box. Once you've copied the code, just paste it into your website or blog to embed it
BBCODE:: 

BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums
wallpaperama | Wallpapers | Forums | Terms Of Service
copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5
copyright © 2013 wallpaperama - All Rights Reserved - Last Updated Mon May 06, 2013 (-8 GMT)
Powered by: Webune Forums V5