Home > MySql, PHP > Connect your site with mysql server

Connect your site with mysql server

Here is a simple [novice type]php script to connect your site with your mysql server.When i start to learning php i use this script to connect my site with my database server.It was so exciting that i can’t tell you.I insert something into a form and that store in database.It was feel like awesome to me.You know the first experience of success.hehehe..

So i give you that.

<?php
$db_host = ‘localhost’;
$db_user = ‘root’;
$db_password = ‘triadpass’;
$db_name = ‘db1’;

function connect_db()
{
global $db_host,$db_user,$db_password,$db_name;

$link = mysql_connect($db_host,$db_user,$db_password);
if(!$link){
die(‘Not connected : ‘ . mysql_error());
}

$db_selected = mysql_select_db($db_name, $link);
if (!$db_selected) {
die (‘Error : ‘ . mysql_error());}
return $link;
}
?>

Here mentioning that localhost was my mysql server address,username was root, password was triadpass and database was db1 for me not for you.You have your own server address,username,password and database name. After entering your server address,username,password and database name name tihs page as config.php.After naming it,you just have to include this page whenever you need.

Like it
<?php
include(“config.php”);
$link=connect_db();
$sql=”select * from table1″;
if(mysql_query($sql,$link))
{
echo “query successful”;
}
?>
ok done

Advertisements
Categories: MySql, PHP
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: