Download presentation
Presentation is loading. Please wait.
1
Server-Side Processing II
2. PHP PHP is a specialised scripting language designed to carry out sophisticated and flexible server-side processing. It uses a mixture of C and Perl style syntax. In makes dynamic web content generation possible. What’s this? 1
2
No, they use some form of dynamic content generation instead.
If you look at major sites like infoseek or cnet or yahoo you will find that they have content which is changing on an hourly basis. Does this mean they have teams of HTML’ers frantically altering the .html pages to update content ? No, they use some form of dynamic content generation instead. This means that the content of the site is separated from the formatting information (the HTML). The content is stored in tables in a database. When the user requests a page, the server (a) retrieves the content from the DB (b) formats it as a HTML page (c) sends it to the user’s browser To update the content, the site owners update the database. No fiddling with HTML. So how is (b) and (c) accomplished?
3
So will ASP (Active Server Pages).
We write a program which queries the database (using SQL commands) and constructs HTML pages on the fly from the results. PHP will do this for us. So will ASP (Active Server Pages). What would such a program look like? Firstly PHP code is embedded inside regular HTML files. Suppose we have created a database which is stored on the server and looks like this. The fields are called name and phone. Homer Simpson Bart Simpson Lisa Simpson Marge Simpson Maggie Simpson
4
<html> <head> <title>Web Database Sample Index</title> </head> <body bgcolor=#ffffff> <h1>Data from Simpsons Table</h1> <? mysql_connect("localhost", "webuser", ""); $query = "SELECT name, phone FROM mytable"; $result = mysql_db_query("example", $query); if ($result) { echo "Found these entries in the database:<ul>"; while ($r = mysql_fetch_array($result)) { $name = $r["name"]; $phone = $r["phone"]; echo "<li>$name, $phone"; } echo "</ul>"; } else { echo "No data."; } mysql_free_result($result); ?> </body> </html>
5
Let’s have a look at this.
<? Indicates to the server that the following code is PHP. Finish with ?>. In this example we are using a database system called MySQL. It’s a free version of something like SQL Server. mysql_connect("localhost", "webuser", ""); This established a connection with the database. $query = "SELECT name, phone FROM mytable"; Variables are preceded with $ symbols. So this sets up a string variable which corresponds to a standard database query. $result = mysql_db_query("example", $query); This executes the query and stores the result in $result. If there was a result we get the following: echo "Found these entries in the database:<ul>"; echo sends the contents of the string to the browser. If you send HTML tags then they will be understood as such.
6
Data from Simpson’s Table Homer Simpson, 234-7575
The while loop then repeatedly fetches rows from the database and stored the results in variables $name and $phone. It outputs them each time as: echo "<li>$name, $phone"; This simply creates a HTML list item with the data and sends it to the browser. The result will be a web page containing: Data from Simpson’s Table Homer Simpson, Bart Simpson, Lisa Simpson, Maggie Simpson, Marge Simpson,
7
So we can update the content of the web page by simply updating the database.
Even better we can combine this with forms by using code like the following. <html> <body bgcolor=#ffffff> <? if (isset($name) && isset($phone)) { mysql_connect("localhost", "webuser", ""); $query = "INSERT INTO mytable VALUES ('$name', '$phone')"; $result = mysql_db_query("example", $query); if ($result) { echo "<p>$name was added to the database</p>"; } } ?> <h1>Add an entry</h1> <form> Name: <input type=text name='name'><br> Phone: <input type=text name='phone'><br> <input type=submit> </form> </body> </html>
8
This links the form to the database in such a way that if values are entered into the form and the submit button pressed, the script is executed an SQL insert command invoked to add a new row to the DB. This means we can easily provide HTML pages to update web sites through a browser …… no knowledge of HTML required! We can issue any SQL commands we like from a PHP script e.g. commands to search the database and we can format the results in any way we like. Big advantage is that we only have to worry about compatibility at the server side. As long as it undertands PHP we’re in business. Great tutorial about this (where this lecture’s examples were taken from) at
9
There are a myriad of other technologies out there for doing this kind of thing (e.g. Microsoft’s Active Server Pages (ASP, ColdFusion, MacroMedia’s drumbeat and so on ….). The combination of PHP and MySQL however is powerful, well-supported, flexible and free! The disadvantage of carrying out processing of any kind on the server is that you have to either (a) have control over your own server so that you can ensure the correct software is present or (b) rent space on someone else’s server which you know contains the correct software.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.