8
(c) Manzur Ashraf----Short Course, KFUPM, KSA The need for dynamic content The Web is no longer static; it's dynamic. As the information content of the Web grows, so does the need to make Web sites more dynamic. Think of an e-shop that has 1,000 products. The owner has to create 1,000 Web pages (one for each product), and whenever anything changes, the owner has to change all those pages. Ouch!!! Wouldn't it be easier to have only one page that created and served the content on the fly from the information about the products stored in a database, depending on the client request? Nowadays sites have to change constantly and provide up-to-date news, information, stock prices, and customized pages. PHP and SQL are two ways to make your site dynamic. PHP PHP is a robust, server-side, open source scripting language that is extremely flexible and actually fun to learn. PHP is also cross platform, which means your PHP scripts will run on Unix, Linux, or an NT server. MySQL SQL is the standard query language for interacting with databases. MySQL is an open source, SQL database server that is more or less free and extremely fast. MySQL is also cross platform

9
(c) Manzur Ashraf----Short Course, KFUPM, KSA Installing Apache server routines Installing Apache is relatively simple. First download the Apache archive, apache_x.x.xx.tar.gz (the latest I downloaded was apache_ tar.gz) from the Apache site and save it in /tmp/src directory. Go to that directory:Apache site # cd /tmp/src/ # gunzip -dc apache_x.x.xx.tar.gz | tar xv #./configure --prefix=/usr/local/apache --enable-module=so # make # make install This will install Apache in the directory /usr/local/apache. If you want to install Apache to a different directory, replace /usr/local/apache with your directory in the prefix. To test your install, start up your Apache HTTP server by running: # /usr/local/apache/bin/apachectl start You should see a message like "httpd started". Open your Web browser and type "http://localhost/" in the location bar (replace localhost with your ServerName if you set it differently). You should see a nice welcome page.

11
(c) Manzur Ashraf----Short Course, KFUPM, KSA Contd. And test your installation by typing: mysql -uroot -p At the password prompt, just press Enter. You should see something like: Welcome to MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 to server version Type 'help' for help. mysql> If you see this, you have MySQL running properly. If you don't, try installing MySQL again. Type status to see the MySQL server status. Type quit to exit the prompt.

12
(c) Manzur Ashraf----Short Course, KFUPM, KSA Installing PHP Download and save the source from the PHP site to /tmp/src:PHP site # cd /tmp/src/ # gunzip -dc php-x.x.xx.tar.gz | tar xv # cd php-x.x.xx #./configure --with-mysql=/usr/local/mysql --with- apxs=/usr/local/apache/bin/apxs # make # make install Copy the ini file to the proper directory: # cp php.ini-dist /usr/local/lib/php.ini Open httpd.conf in your text editor (probably located in /usr/local/apache/conf directory), and find a section that looks like the following: # And for PHP 4.x, use: # #AddType application/x-httpd-php.php #AddType application/x-httpd-php-source.phps Just remove those #s before the AddType line so that it looks like: # And for PHP 4.x, use: # AddType application/x-httpd-php.php.phtml AddType application/x-httpd-php-source.phps

15
(c) Manzur Ashraf----Short Course, KFUPM, KSA Test whether you have PHP installed properly Type the following code in a text editor and save it as test.php in a directory accessible by your Web server:

24
(c) Manzur Ashraf----Short Course, KFUPM, KSA Opening and closing databases $link=mysql_connect("server","username"," password"); Next, the mysql_select_db command can be used to select a database on the current server : mysql_select_db("db_name",$link); Last but not least, the mysql_close command closes an open database connection : mysql_close($link);

25
(c) Manzur Ashraf----Short Course, KFUPM, KSA Submitting queries $result=mysql_query("SELECT * FROM CONTACTS",$link); This sends a query (in the SQL language) to the database server specified by the link identifier ($link in the example). You can omit the identifier to use the last connection you made, which is sufficient unless you are maintaining more than one connection to different servers. A pointer to the results of the query is returned and stored in $result as seen in the example above. An alternate command selects a database and makes a query: mysql_db_query("SELECT * FROM CONTACTS","db_name",$link); Using this command eliminates the need for the mysql_select_db command, and is useful if you are working with a number of databases at once. As with the previous command, the link identifier ($link in the example) is optional.

27
(c) Manzur Ashraf----Short Course, KFUPM, KSA More on retrieving data Another useful command allows you to determine the number of rows in a query's result: $rows=mysql_num_rows($result); You can use this information to create a loop to display the results or to simply use a while loop without knowing the exact number of rows. After you have finished using a query result you can use the following command to free the memory used by the result: mysql_free_result($result);

28
(c) Manzur Ashraf----Short Course, KFUPM, KSA Creating and deleting databases $success=mysql_create_db("db_name",$link); > This attempts to create a database called db_name on the server connection identified by the $link variable. As before, the link identifier is optional. This returns a boolean value that indicates whether the operation was a success. > You can also delete an existing database using the mysql_drop_db command: $success=mysql_drop_db("db_name",$link); This attempts to c">
This attempts to create a database called db_name on the server connection identified by the $link variable. As before, the link identifier is optional. This returns a boolean value that indicates whether the operation was a success. > You can also delete an existing database using the mysql_drop_db command: $success=mysql_drop_db("db_name",$link);">
This attempts to c" title="(c) Manzur Ashraf----Short Course,2004 ---- KFUPM, KSA Creating and deleting databases $success=mysql_create_db("db_name",$link); > This attempts to c">

31
(c) Manzur Ashraf----Short Course, KFUPM, KSA The ORDER BY clause SELECT * FROM Contacts ORDER BY contact_name ( by default, ASC) SELECT * FROM Contacts ORDER BY contact_name DESC SELECT * FROM Contacts WHERE num_sales > 10 ORDER BY contact_name

38
(c) Manzur Ashraf----Short Course, KFUPM, KSA Case Study A popular feature of many Web sites is a running list of news items called a weblog. This often appears as a list of announcements on a company home page, or even by itself in sites dedicated to news or journals. While you can create this type of page manually in HTML, using a database makes it easy to add entries to the log, and PHP can be used to display the most recent entries. The following pages will guide you through the process of creating the PHP and HTML files used in this application. To try it out yourself, you will need access to a Web server that supports PHP 3.0 or later, and a user name and password for a MySQL database server. If you have a different database server, you can easily adapt the PHP code for use in another database format. In the PHP scripts on the following pages, be sure to replace the generic server_name, user, password, and db_name entries with the correct information for your server.

54
(c) Manzur Ashraf----Short Course, KFUPM, KSA Syntax visiting.. "SELECT * FROM personnel WHERE $searchtype LIKE '%$searchstring%' ORDER BY firstname ASC" Two news things are introduced here, "LIKE" and "ORDER BY". LIKE simply means 'sounds like'. The '%' sign represents any possible combination of characters (numbers or letters). So to find people whose first name starts with 'J' we would use the SQL command "SELECT * FROM personnel WHERE firstname LIKE 'J%” To find those people with a name ending with J we have to use '%J'. If we wish find people with 'J' anywhere in their name (first, middle, or last) we have to use '%J%'.