PHP Database Driven Website
PHP Database Driven Website
You can use PHP, (in conjunction with SQL and HTML), to create database driven websites.
To create a database driven website in PHP, you need a database management system (DBMS). Common database systems include MySQL, Microsoft SQL Server, and Oracle.
Your DBMS can be located either on the same computer that the website is on, or on another server. It's good practice to separate your database server from your web server, but if you've only got one machine to develop on, sharing the same machine shouldn't cause any problems (as long as it's powerful enough to run a web server and database server etc).
Anyway, once you have a database with some tables and some data, you can connect to it and query it.
MySQL is a database system commonly used with PHP websites. The following examples demonstrate how to connect and query a MySQL database.
Connecting to the Database
Before you can query your database, you need to connect to the database server, then locate the database. Once you've done this, you can send in your SQL code to do your queries.
To connect to the database server:
mysql_connect("localhost", "web_user", "LetMeIn!") or die(mysql_error());
The above code uses the mysql_connect function to connect to the database server. We provide the following parameters: Server, Username, Password. PHP needs this info so that it knows which server to connect to. In this example we are just connecting to the local machine so we use "localhost" as the server.
We have also used the PHP die and mysql_error functions to be used in the event there's an error and PHP can't connect to the server. This will display the error message which can assist us in determining the cause of the problem.
To select the database:
mysql_select_db("MyDatabase") or die(mysql_error());
The above code uses the mysql_select_db function to select the database from the database server. You need to do this because, your database server could contain many databases. You need to tell PHP which database to use.
Again we use the die and mysql_error functions to be used in the event of an error.
Querying the Database
You can use the mysql_query function to send a SQL query to the database:
$result = mysql_query("SELECT * FROM Individual")
or die(mysql_error());
What we do here is, assign the results of a query to the $result variable. The query is acheived by passing a SQL statement to the mysql_query as a parameter. In this SQL statement, we are selecting all records from the "Individual" table.
Once again we use the die and mysql_error in case there's an error.
Displaying the Results
To display the results, you need to loop through the results of the query and display each record with each iteration of the loop:
while($row = mysql_fetch_array($result)){
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}
Here we use a while loop to loop through the results of the query. The while loop keeps iterating until it finishes the last result. This means we can display each result as it iterates past that result. We display the result using $echo and the PHP $row variable, indicating which columns we want to display.
The Whole Code
Combining the above code, (and adding comments), results in something like this:
<?php
// Connect to the database server
mysql_connect("localhost", "web_user", "LetMeIn!") or die(mysql_error());
// Open to the database
mysql_select_db("MyDatabase") or die(mysql_error());
// Select all records from the "Individual" table
$result = mysql_query("SELECT * FROM Individual")
or die(mysql_error());
// Loop thru each record (using the PHP $row variable),
// then display the first name and last name of each record.
while($row = mysql_fetch_array($result)){
echo $row['FirstName']. " - ". $row['LastName'];
echo "<br>";
}
?>