Faculty of Computer Science and Information Technology BIC 21203 | Web Development Semester II 2012/2013 _____________________________________________________________________________________ LAB SHEET 9 Title : Database connection, Array and Control Statement Objective: At the end of this lab, student should be able to use PHP for: i. Applying functions offered by PHP in database connection ii. Establishing database connection iii. Selecting database to be used in web application iv. Calling PHP code in external file v. Using control statement to customized HTML output Exercise 1: Connecting to MySQL database Before you can get content out of your MySQL database, you must know how to establish a connection to MySQL from inside a PHP script. The function to connect to MySQL is called mysql_connect(). 1) Create new .php document named config.php Note: You can create the document using any editor. Ex: Macromedia Dreamweaver, Notepad 2) Type in the following PHP code in config.php Prepared by: Madam Hazwani binti Rahmat Faculty of Computer Science and Information Technology BIC 21203 | Web Development Semester II 2012/2013 _____________________________________________________________________________________ While writing your PHP program you should check all possible error condition before going ahead and take appropriate action when required. This can be done using the die function as it provides debugging functionality. This function prints a message and exits the current script. If the database connection fails, an error message will be generated. If this case happened, check the parameters value used in mysql_connect() function. Make sure your password, username and hostname are correct. Note: You can customize the error message in or die function according to your needs. In case you have set a username and password for database, change the value of $user and $password according to your database access. Exercise 2: Selecting database Once you've connected, you're going to want to select a database to work with. For this, we'll need the mysql_select_db() function: 1) Add in the following PHP code in config.php Note: You complete code should look like the following figure Prepared by: Madam Hazwani binti Rahmat Faculty of Computer Science and Information Technology BIC 21203 | Web Development Semester II 2012/2013 _____________________________________________________________________________________ 2) Run the code in your web browser. You should get an error message prompts database could not be selected. The reason is that, the database name provided in the mysql_select_db() function has not been created yet. 3) Change the database name is mysql_select_db() function to your project database. 4) Run the code again in your web browser. You should get a blank screen (no error messages) as the connection has been successfully established and the database has been connected. Exercise 3: Calling PHP code from external file The include() function takes all the text in a specified file and copies it into the file that uses the include() function. Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file. 1) Create new .php document named index.php 2) Type in the following PHP code in index.php Prepared by: Madam Hazwani binti Rahmat Faculty of Computer Science and Information Technology BIC 21203 | Web Development Semester II 2012/2013 _____________________________________________________________________________________ Note: The PHP codes in config.php are needed in pages that manipulate your database content. Hence, you will need the include() function to call the config.php at the top of every page that involve database interaction. Exercise 4: Using Array, for loops and if…else if…else statement 1) Create a new php document named array.php 2) Create an array named patient which stores THREE patient names; James, David and Lucas. 3) Using looping for, display the array elements in an HTML table as in following figure: Prepared by: Madam Hazwani binti Rahmat Faculty of Computer Science and Information Technology BIC 21203 | Web Development Semester II 2012/2013 _____________________________________________________________________________________ 4) Using if…else if…else statement, evaluate the patient age (variable named age) according to following condition and statement: Condition: Name equals to James Statement: Age equals to 6 Condition: Name equals to David Statement: Age equals to 31 Condition: Name equals to Lucas Statement: Age equals to 1 Exercise 5: Using While loop and if statement Convert exercise for using while loop and if statement Exercise 6: Relocate user to new page header() function is used to transfer user to another html / php page without having to click a link. This exercise will create a jump.html page with link to relocate.php. The relocate.php (which uses the header() function) will redirect user to array.php in exercise 4. 1) Create a new html file named jump.html and php file named relocate.php 2) In jump.html, create an HTML link named “Jump from here” which links to relocate.php 3) Type in the following code in relocate.php 4) Run your program in the web browser. Prepared by: Madam Hazwani binti Rahmat Faculty of Computer Science and Information Technology BIC 21203 | Web Development Semester II 2012/2013 _____________________________________________________________________________________ Exercise 7: Adding functionalities to your project Now that your HTML pages has been connected to your and MySQL database, you may proceed adding functionalities to your web project. Prepared by: Madam Hazwani binti Rahmat
© Copyright 2026 Paperzz