Php Script To Test Mysql Database Connection Check Db Connect
php script to test mysql database connection check db connect
php, script, to, test, mysql, database, connection, check, db, connect
Php Script To Test Mysql Database Connection Check Db Connect
Post Description: php script to test mysql database connection check db connect
POST# 424
Posted On: Fri Feb 29, 2008 12:01 pm
web hosting
Topic: 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>




Thu Jul 05, 2007 10:00 am
1
tebakken
Reply #1009
parse error: syntax error, unexpected t_string, expecting ',' or ';' on line 89
Sat Jul 14, 2007 3:16 pm
2
Ali
Reply #1010
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.

Sun Oct 07, 2007 3:44 pm
3
edwino85
Reply #1011
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..

What do you think?

* name:  

* email:  

* Please enter comments:


Receive Replies on my Comments
(An email will be sent to you when someone replies to your comments)

Add image to comments
yes no             upload