All Education of Pakistani colleges, schools, universities, foreign study, scholarships, results, admissions and courses. Intermediate English Notes, Good-bye Mr. Chips, Online Taleem, Taleem Microsoft, Management Sciences, Computer Sciences Free tutorials, C++ Library, MS Access, Internet of Things, Rural Marketing, Angular2, Framework7, Firebase, Design Thinking, Internet Security, Excel Charts, Computer Security, Conversion Rate Optimization, Business Law, Laravel, SQL Server, Windows10
Saturday, October 24, 2015
Tuesday, March 5, 2013
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:
Code |
---|
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:
Code |
---|
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:
Code |
---|
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:
Code |
---|
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:
Code |
---|
Database Summary
So, you've made it to the last page of this database tutorial... Well done!
You should now have a general understanding about databases and how they're used. This tutorial was intended for beginners trying to gain an understanding of databases. Databases are not like most other files and require a little bit of thought in order for you to understand the concept.
If you have your own database management system installed, you should have a better understanding of where to start and what the various options mean.
What Next?
Most of the examples in this tutorial used Microsoft Access.
Also, you'll have noticed SQL coming up throughout this tutorial. SQL is a very powerful language, but is also very easy to learn. You can achieve a lot even by learning just a little SQL. Once you complete this tutorial, you will be able to do things such as:
- Select only the columns you want from a query
- Query multiple tables
- Create databases programatically
- Create tables programatically
- Query multiple tables
- Use built-in functions
- Create an index
- And more...
Database Driven Website
A database driven website is a website that has most of its webpage content in a database. Therefore, the website content isn't actually sitting in files on the server, it is sitting in tables and columns in a database.
A website with its content stored on the file system is often referred to as a static website, whereas a database driven website is often referred to as a dynamic.
Content Management Systems
A website with dynamic content usually has a CMS (Content Management System) to assist the content providers in updating the website.
A CMS is usually provided in the form of an administration area where content providers need to log in before they can add content. Once logged in, they can create, update and delete articles. They may be able to upload files such as Word documents, PDF files etc. They might be able to upload images too.
All of this content can be stored in the database. Some may be stored on the file system too though. For example, although documents and images can be stored in the database, there are sometimes reasons to store them on the file system. Performance is often a key reason. Database size is another.
Discussion Forums and Blogs
Discussion forums and blogs have become a popular feature for many websites. Most, if not all, forums and blogs are database driven. Users can register their details, then add content. When the user clicks the "Submit" button, their details/content is inserted into the database. Then when someone decides to view this content, it is read from the database using SQL (Structured Query Language).
Combination of Static and Dynamic
Some websites have a combination of static content and dynamic content. There could be any number of reasons for this. Often, smaller websites will be static. There's little need to configure a database just to store a handful of webpages - much easier and cheaper to keep them as files on the server. Even websites like this might contain some added functionality such as a discussion forum, or a blog. In this case, the discussion forum or blog will need its content stored in a database.
Benefits of a Database Driven Website
Database driven websites can provide much more functionality than a static site can. Extended functionality could include:
- Enabling many (potentially non-technical) users to provide content for the website. Users can publish articles on the website without needing to FTP them to a web server.
- Shopping cart
- You can provide advanced search functionality that enables users to filter the results based on a given field. They can then sort those results by a field - say "Price" or "Date".
- Customized homepage
- You can allow your users to perform tasks such as registering for a newsletter, post questions to your forums, provide comments on a blog, update their profile, etc.
- Integration with corporate applications such as CRM systems, HR systems etc
- Much more
Creating a Database Driven Website
The most common tasks for database driven websites is inserting, updating, and deleting data. Some of these are the same tasks that you learned in this tutorial, however when using a database driven website, you need to use a different method to do these tasks. You need to use a programming language called SQL (Structured Query Language) to insert, update, and delete your data.
Don't worry, this is not as scary as it may sound. SQL is a very easy language to learn and, once you start using it, you will be thankful you took the time to learn it. In fact, you've already learned some basic SQL statements in previous lessons.
To create a database driven website, you need the following skills:
- You need to be able to build a static website HTML, and preferrably CSS and JavaScript
- You need to be able to write basic code using a server side scripting language such as PHP, ColdFusion etc
- You need to know how to write basic SQL
Subscribe to:
Posts (Atom)