Archive

Archive for the ‘MySql’ Category

Connect your site with mysql server

June 29, 2009 Leave a comment

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

Categories: MySql, PHP