Working with Database

Xpert

202.***.***.***
1,949 days ago

Working with Database

Overview: In this section you will learn how to make PHP work with databases.

Okay I admit this is what most of you were waiting for creating a database driven site using php. Initially we will be working with MySQL database, but don't worry its very easy to connect to other databases. I will show you how to connect to other databases later on.


Syntax for connecting to a MySQL database
PHP has built in support for connecting to MySQL. The following PHP function call establishes the connection:

mysql_connect(address, username, password);

address: It is the IP address or hostname of the computer on which the MySQL server software is running. If MySQL is running on the same machine on which PHP is installed we use "localhost".

username: As the name suggests is the MySQL user account name.
password: Contains the password of the MySQL database.

The mysql_connect function shown above, returns a number that identifies the connection that has been established. Since we intend to make use of the connection, we should hold this value in a variable. lets assume that our MySQL database is on a local machine and the username is root and password is pass and we will be storing the connection in a variable $db. Then the mysql_connect function will look something like this

$db = mysql_connect("locahost","root","pass");

Now that we are connected to MySQL database the next step is to select the database we wish to use to select the database we use the function mysql_select_db the syntax of which is given below

mysql_select_db(Database name,database connection string);

Let assume that we will be connecting to mybuddy database, so you code will be

mysql_select_db("mybuddy",$db);

That is it we have connected to MySQL database, see it so simple its just 2 lines of code

$db = mysql_connect("localhost","root","pass");
mysql_select_db("mybuddy",$db);


Before we can start retrieving data from our database we need some tables with some data in our database so that we can work with them!

Bradd

203.***.***.***
1,931 days ago
Do you have some other codes regarding other database like oracle or sql server ? i know those are not freewares but we should have knowlege about those so please reply here with other database intrfaces!