Databases
Adding a database to a website can provide the means for great dynamic content, all kinds of user interactivity .
A database is a collection of data that is stored independently of the manner in which you collect it or may wish to retrieve it.
For example, an address book.
Typically, databases store information in tables. Tables organize data into rows and columns. Each row represents a single record, such as one name and address. Each column holds a part of the record, such as a first name, or a phone number. With such an arrangement records could be rapidly resorted based on the values found in any column (or combination of columns) just like you can in a spreadsheet program.
Imagine a table that holds lots of names and cities , phone number etc.
Now imagine another table that holds information about lots of cars, including manufacturers .
Now imagine that each car record is linked to the name of the car's owner .
With these links in place it would be easy to use our sort/retrieve mechanism to provide a list of people in NY that own a Ford .
Extracting information from a database in this way is known as querying the database.
The most widely used database with PHP is Mysql (which is also free).
Most common database operations are create, drop , insert , update ,select .
The first thing you'll want to do is to create a database . That can be easily do using phpmyadmin or another similar tool.
Then you have to establish a connection to your MySQL database server. This is accomplished with the mysql_connect function. Here's an example:
$conn = mysql_connect ('servername' , 'username' , 'password');
$conn is an identifier (a positive integer, returned by the mysql_connect function) that identifies this connection and is used in subsequent function calls to tie those calls to this connection. The mysql_connect function is provided three parameters, the name of the server (commonly 'localhost' when the server is on the same computer as PHP), the username and password authorized to access the server.
Next, we need to check that the connection was successfully established. For the sake of this example we are going to print a message on the screen to let us know of whether or not the call was good. In practice, you will probably take a different course of action .
Another common real world practice is to use the die function to terminate the program and display a message.
$conn = @mysql_connect ('servername' , 'username' , 'password') or die("Connection to the DBMS server failed");
to select a MySQL database in PHP use the mysql_select_db ( ) function, like this:
mysql_select_db ("mydatabase");
Now that we are connected to the server and selected a database we can run queries.
Let's say we have a table named Info having the followinf fields : name, address, email and we want to display all records.
$sql="select * from Info";
We have stored the sql create query in a variable $query and we will pass this as a parameter to the function like below.
$rez=mysql_query($sql,$conn);
The above command will execute the query ( stored in variable $sql) and we can check the status of the query ( successful or not ) by checking the status of $rez. $rez will be true of the query is successfully executed or it will return false.
If the result was true we must use variable $rez to populate an array with matching records. Each field can be accessed using array name and field name from database as index.
while($row=mysql_fetch_array($rez))
{
echo $row['name'];
}