MySQL Connectivity., Tutorial : How to connect - PHP / MySQL Tutorials
 
  Buy Ebooks, Video Games, DVDs, Electronics and more !
Home | Help Forums | Web Design Tutorials | Free Swishmax Downloads Make Money Online Blog . Free Image and Photo Sharing and Hosting

 

 

Company

Home

Nework Details

Tutorials

SWiSH 2

SWiSHMax

Flash MX

PHP & MySQL

HTML / JScript

Adobe Photoshop

Corel Draw

Gimp

Miscellaneous

Downloads

Templates

Plugins

Fonts

Wallpapers

Free Images

Scripts and Codes

Products

E-Books

SWiSH BB

SWiSH Templates

SWiSH-DB Newsletter
Subscribe to our newsletter : GO
  Partners / Affiliates

   Free image hosting
   Web Design Pakistan
   Swish Templates
   SWiSH Climax
   Talkfreelance Community
   Template Desire
   Lite boards
   Script Sector
   GimpTalk
   13dots
   Idea Designs
   PHP Cafe
   Sposatoettore
   Free swishmax templates
   Swishzone

Manage your site with Flax Article Content Management System
MySQL Connectivity., Tutorial : How to connect
Category : PHP / MySQL | Level : Beginner | Language : English
Ask Question | Read Comments | Post tutorial | Previous | Next

Web www.swish-db.com

Very Basic Tutorial which will demonstrate how to connect with MySQL.

LEVEL : BEGINNER

Every database has its users and password. So to connect to a database you must know Database name, username and password running on a host. You cannot perform any other functions unless the connection to databse is established. If you have already used the mysql_connect() function to establish a connection and again you try to connect with the same function, it will not establish a new connection, but it will return the old connection which is already established.

Syntax : mysql_connect(Char HOST, CHar USER, Char Password);
Example : mysql_connect("localhost","username","password");

By using mysql_connect(), the connection is established, but now you have to choose a database to work on, for example there many other databases on the system, so you have to select the appropriate one, on which you will be working. So for example the database I wish to select is swishdb. So I will use the following function right after the mysql_connect()..

mysql_select_db("swishdb");

But its good approach to define your variables somewhere in configuration
files such as :

$dbhost = "localhost";
$dbname = "swishdb";
$dbuser = "ali";
$dbpass = "roman";

and use them like this.

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());


The function die() will not let the upcoming procedure or function work or execute if any error occurs. mysql_error() function displays the error occurs. There is another function which also displays the error number that is mysql_errno(). So if you are making all in one file named as config.php then it will look like this.

<?
$dbhost = "localhost";
$dbname = "swishdb";
$dbuser = "ali";
$dbpass = "roman";
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
?>


So this is all about how to connect to database. There are only 2 point, one is to establish connection with database with mysql_connect() and the second point is to select datatase using mysql_select_db();

I hope you find this tutorial useful. This is only written for the newbies who learn to connect to database, I will be writing some more detailed tutorials.

regards
- Ali..


© 2002-2005 Flaxweb Network | Article Manager | Gimp Tutorials Library | RAD C++ Library | Free C/C++ Sourcecode |
Sites of interest : Web Design Blog | Swish Templates | Swishmax Ebook | Photoshop Templates | Gimp Tutorials | Text Forum | Make Money Online