php script to test mysql database connection check db connect
Post Description: php script to test mysql database connection check db connect
Tags: php, script, to, test, mysql, database, connection, check, db, connect
This Post Was Posted On Feb 29, 2008 By web hosting #1265
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
<!--
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>
parse error: syntax error, unexpected t_string, expecting ',' or ';' on line 89
errors anymore.. thanks for posting bugs..
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.
Powered by: Webune Forums V3