|
|
|
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..
|
|
|
Visitor's 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
|
|