Php Script To Test Mysql Database Connection Check Db Connect



Php Script To Test Mysql Database Connection Check Db Connect
 (381) dminister a web business
Php Script To Test Mysql Database Connection Check Db Connect
Post Description:
Post Tags: php, script, to, test, mysql, database, connection, check, db, connect
This Post Has Been Viewed 540 Times Since Tue Jul 03, 2007 9:41 am Posted By hostman with 3 replies
Php Script To Test Mysql Database Connection Check Db Connect
i wrote this little piece of code (snippet) to show you how you can check for data connection on your scripts. lets say you have a script and you want to confirm that you are able to connect to the database with the username, password and hostanme that a user has provided. you usually see these type of form on install scripts. well, i was just writing an install script and i thought i put this php snippet here just incase someon might find it useful. all you have to do is copy and paste this code into your texteditor like notepad and save the file as connection.php and upload to your server, then the form will ask you for your database hostname, username, password
<style type="text/css">
<!--
form#checkout div.row div.field
{
float: left;
width: 150px;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
div.clear
{
clear: both;
}

b {
color: #000000;
}

-->
</style>
<h1>This script provided by <a href="http://www.webune.com">Webune.com</a></h1>
<hr>
<?
ini_set('display_errors', 0);

$field_name = array(

1 => 'Database Server:',
2 => 'Username:',
3 => 'Password:',
4 => 'Database Name:'

);
$field_type = array(

1 => 'text',
2 => 'text',
3 => 'password',
4 => 'text'

);

function tidy ($form_string) {
$form_string = trim(stripslashes($form_string));
return $form_string;
}

function form($error) {
?>

<form action="" method="post" id="checkout">
<?
$num_fields = 4;
global $field_name, $field_type;

if($error){
echo '<h1 style="color: red">'.$error.'</h1>';
}

for($counter =1; $counter <= $num_fields ; $counter++)
{

echo '<div class="row">
<div class="field">'.$field_name[$counter].'</div>';

switch($field_type[$counter])
{
case 'textarea':
echo '<textarea name="field['.$counter.']">'.$_POST['field'][$counter].'</textarea>
';
break;

case 'checkbox':
echo '<input type="'.$field_type[$counter].'" name="field['.$counter.']" value="'.$_POST['field'][$counter].'">';
break;

case 'radiobutton':
echo '<input type="'.$field_type[$counter].'" name="field['.$counter.']" value="'.$_POST['field'][$counter].'">';
break;

case 'select':
echo'
<select name="question" >
<option value="What is your favorite pet name?">What is your favorite pet name?</option>
<option value="What is your father birth date?">What is your father birth date?</option>
<option value="What is your favorite music band?">What is your favorite music band?</option>
<option value="What is your mother maiden name?">What is your mother maiden name?</option>
</select>
';
break;

case 'password':
echo '<input type="'.$field_type[$counter].'" name="field['.$counter.']" value="'.$_POST['field'][$counter].'">
';
break;

case 'file':
echo '<input type="'.$field_type[$counter].'" name="field['.$counter.']" value="'.$_POST['field'][$counter].'">';
break;

case 'hidden':
echo '<input type="'.$field_type[$counter].'" name="field['.$counter.']" value="'.$_POST['field'][$counter].'">';
break;

case 'break':

break;

default:
echo '<input type="'.$field_type[$counter].'" name="field['.$counter.']" value="'.$_POST['field'][$counter].'">';
break;
}

echo'</div>
<div class="clear"></div>
</div>';

}
?>
<input type=submit name="Submit" value="Submit" />
</form>
<?php
}
// The data comes from the form in fig 4.2

if(isset($_REQUEST['Submit']))
{

# check all fields are not empty, if so, $error is true
foreach($_POST['field'] as $field => $line) {
$value = tidy($line);
if(!$value){
$error = $field_name[$field].' is empty <br>';
break;
}
//print("n<br />field[" . $field . '] ' . $value);
}

$server = trim(stripslashes($HTTP_POST_VARS['field'][1]));
$username = trim(stripslashes($HTTP_POST_VARS['field'][2]));
$password = trim(stripslashes($HTTP_POST_VARS['field'][3]));
if(!mysql_connect($server, $username, $password)){
$error = 'Unable to connect to database...';
}

if($error){
form($error);

}
else{

echo '<h1 style="color: green">Success!</h1>Connection to database successful. <a href="?step=2">Continue >> </a>';
}
}
else {
form($error);
}
?>
<hr>
We hope you found this little script usefull. if you ever need php hosting, visit our friends at <a href="http://www.webune.com">www.webune.com</a>


Leave Your Comments     [ dejar commentarios ]
  * Name     [nombre]

  * eMail (will not be published)     [coreo electronico]

* Enter Your Reply or Comments:    [commentarios]


Add Picture To Comments         [incluir foto]
YES NO             upload
Receive Replies on my Comments (An email will be sent to you when someone replies to your comments)

     

Comments and replies About Php Script To Test Mysql Database Connection Check Db Connect




:: 1 :: #9736 - Reply By tebakken On Thu Jul 05, 2007 10:00 am
parse error: syntax error, unexpected t_string, expecting ',' or ';' on line 89
:: 2 :: #10876 - Reply By Ali On Sat Jul 14, 2007 3:16 pm
just remove the '
from the option field

> that is from : pet's, father's, mother's



otherwise nice script. really helped me see that i was an idiot typing in the wrong db info several times.

:: 3 :: #23314 - Reply By edwino85 On Sun Oct 07, 2007 3:44 pm
ok, i corrected the errors, you shouldn't get

parse error: syntax error, unexpected t_string, expecting ',' or ';' on line 89

errors anymore.. thanks for posting bugs..