A Function for Connecting to MySQL Server

Written by @m_k_amin 21 November 2012

The first step to working with the databases is craeting connection. You can do it within your codes, but professional programmers handles events by functions. Here is a function to connect to MySQL server. This function returns true in success and false, otherwise. It also has capability to print the success or failure message. Enjoy!

Code Snippet:

                                                
                                                <!-- this script is provided by https://www.phpfreecode.com coded by: Kerixa Inc. -->
<!-- This Script is from www.phpfreecpde.com, Coded by: Kerixa Inc-->
<?php
if (DbConnect('localhost','root','')) echo ('
	<p style="text-align: center; font-size: 20pt; color: #000000">Continue your 
	Coding . . .</p>
	');

function DbConnect($host,$user,$pass){
	$host="localhost"; // Host name
	$username="root"; // Mysql username
	$password=""; // Mysql password
$link=mysql_connect($host, $username, $password);
if(!$link){
	echo('
	<p style="text-align: center;	font-size: 20pt;"><span style="color: #FF0000;">Failed to connect to the database! </span>
	<br><span style="font-size: 12pt;">&gt;&gt;Please check the parameters and database server&lt;&lt;</span></p>
	');
	return false;
}else {
	echo('
	<p style="text-align: center; font-size: 20pt; color: #1CB00E">Connecting to 
	the Database was Successful!</p>
	');
	return true;
}}
;
?>
<br style="font-size: 8pt"><font face="Tahoma"><a target="_blank" href="http://www.phpfreecode.com/"><span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font></div>
<a target='_blank' href='https://www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                            

Example:


About @m_k_amin

This user is pending a biography.

M

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2