Simple PHP MySQL Connection Test Script Example

RegisterLogin
Simple PHP MySQL Connection Test Script Example
Post Description: Simple PHP MySQL Connection Test Script Example
Tags: simple, php, mysql, connection, test, script, example
This Post Was Posted On Nov 10, 2009 By web hosting #1489
Hello, Welcome to Webune Hosting Support Forums. We have excellent service and provide the best web support. If you are a web master or a web programmer, you will find this short tutorial very helpful. 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 can download the attached file for the full script, or you can created youself...



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
###################################### 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 Script 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 = 'ERROR: Please Provide Database Name.';
		return false;
	}
	if ($db_error) {
		return false;
	} else {
		if (!@db_select_db($database)) {
			$db_error = 'ERROR: Cannot Connect to Mysql Server. The Error is:<br>'.mysql_error().'<br>TIP: Check Username and/or Password';
			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']; ?>"> (IP Address [ or enter <u>localhost</u> if not sure])</td>
</tr>
<tr>
<td><div align="right">mysql username:</div></td>
<td><input type="text" name="username" value="<?php echo $_REQUEST['username']; ?>"> (MySQL User)</td>
</tr>
<tr>
<td><div align="right">mysql username password:</div></td>
<td><input type="text" name="password" value="<?php echo $_REQUEST['password']; ?>"> (MySQL User's Password)</td>
</tr>
<tr>
<td><div align="right">mysql database name:</div></td>
<td><input type="text" name="database" value="<?php echo $_REQUEST['database']; ?>"> (Database Table you want to test)</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
<?php
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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">Continue &gt;&gt; </a></strong>';
				}
		} else {
			$error = "ERROR: please provide a hostanme";
			echo step1($error);
		}
	break;

	default:
	echo step1('Step 1... Please Enter All Fields');
	break;
}
?>
<div align="center"><img src="http://www.webune.com/images/headers/default_logo.jpg" alt="Webune Hosting"></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
Download File: testsql-5702.zip
Leave Your Comments
Related Pages: [Add Your Website]
Post New Topic
webune
#4446 1
1. be sure you allow cookies from webune on your browser.

2. be sure your firewall is not blocking sessions. (most likely)

while firewalls are great tools to protect your computer. however, they create what is called, false positives, which simply means that this occurs when the intrusion-detection or firewall system detects a legitimate action as a possible intrusion
Sep 16, 2010 Reply Report abuse
samsoramor
#4434 2
your web so crazy. i register and login and i still can not download how comes? so dumb.
Sep 13, 2010 Reply Report abuse
Can Surmeli
#1928 3
thank u so much. nice work. it really helped me.
Feb 24, 2008 Reply Report abuse
hostman
#1927 4
make sure you have php mysql in your website. check with your host.
Feb 23, 2008 Reply Report abuse
jj
#1926 5
i got a syntax error on line 76
Feb 23, 2008 Reply Report abuse
Leave Your Comments...
©2012 Webune Forums - Fri Dec 14, 2012 2:02 am
Powered by: Webune Forums V3