Connect to MySQL database

This tutorial will show you how to connect to mysql database. It's easy to write a script to connect to world's most popular open source database.

Syntax

mysql_connect("host", "username", "password")or die("cannot connect to server");

Overview

Define your database information, in this example use information of www.phpeasystep.com

* host="localhost" you don't have to change it. When it's on your computer or server it still be localhost

Username = phpeasystep
Password = 1234
Database = test

Example

$host="localhost";
$username="phpeasystep";
$password="1234";
$db_name="test";

mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select db");

or

mysql_connect("localhost", "phpeaststep", "1234")or die("cannot connect to server");
mysql_select_db("test")or die("cannot select db");

Creating file config.php

$host="localhost";
$username="phpeasystep";
$password="1234";
$db_name="test";


mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select db");


Save this code as file "config.php", connect.php or whatever you want. When you want to use this code include it to your main php file

Example

<?php

include("config.php");

$tbl_name="member";
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
...

?>

Similar Post
Order MySQL results

Order MySQL results

In this tutorial, you'll learn how to sort MySQL result. You can sort MySQL result by ascending, descending and random MySQL result.
Managing MySQL database with phpMyAdmin

Create database, table and managing MySQL database using phpMyAdmin

phpMyAdmin is a tool for managing MySQL database and free of charge, it's a web base tool. This tutorial you'll learn how to create database, create table include export(backup) MySQL database.