Web Development Logs
Find articles, news and reviews about latest technologies.


Welcome Guest !
please login or register a new free account.


Home | Archives | Submit Article | Top Rated | Advance Search | Contacts Us | Rss Feeds

    Main Categories
» Free downloads
» General Blog
» Google Adsense
» Scripting languages
» Search Engines
» Web Designing

  More Options
» Most read articles
» Most rated articles

   Subscription
Subscribe now and receive free articles and updates instantly.
» Your name » Your Email

titles description    advance search
Published : April 29, 2006 | Author : Ali Roman
Category : Scripting languages | Total Views : 913 | Rating :

  
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..



1 2 3 4 5
please rate this article     Poor
Excellent    
Most viewed articles in Scripting languages category

Retrieving data from MySQL database using php
Creating simple contact form using swishmax and php
Creating dynamic combo list with PHP and MySQL
Creating simple percentage preloader with swishmax
Saving data to a text file with swishmax and php
Most recent articles in Scripting languages category

Saving data to a text file with swishmax and php
Creating dynamic combo list with PHP and MySQL
Retrieving data from MySQL database using php
Establishing connection with MySQL using php
Creating table using MySQL database

 Visitor's Comments !

there are no comments...


    Random Pick
well, I think its enough for today, I have added a couple of oldies but goldies articles which I posted long way back on my community, some of them are still very useful.

    Statistics
» Total Articles
69
» Total Authors
85
» Total Views
98658
» Total categories
6

Delete cookies set by this site | Top   

Article Manager by Flaxweb
Copyright 2006 © Flax Article Manager v1.1