All PHP Tutorials
Ad Management
Counter
Email System
Forum and Guestbook
File Upload
Image Manipulation
Login / Members / Password
Pagination
Encode Script
Refresh / Redirection
Miscellaneous
All MySQL Tutorials
Create, Manage Database using phpMyAdmin
Connect to Database
Insert Data
Select Data
Edit Database
Update Database
Delete Database
Order Results
Forums
Home > MySQL Tutorials
Connecting to MySQL database
Connecting 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 opensource 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


Test
$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 file as "config.php"
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);

?>

Random Tutorial
 
Upload and Rename File
In this tutorial show you how to rename file when upload file to server to prevent new file overwrite an old file that exists in you server. 
 
Limit Displayed Characters From Your Text
This php script helps you limit displaying characters form you message and not cut out your word. 
Advertisement
 
   
Hosted by Hostgator
© PHPeasystep.com 2005-2008