Web Application Dev. PHP & MySQL PDF
Document Details
Uploaded by StrongestBanjo
Hormuud University
Ali Abdi Mohamed
Tags
Summary
This document provides an introduction to web application development using PHP and MySQL. It covers course objectives, technologies, and a description of the course. The outline includes various chapters on the topics, and the document also includes information about HTTP and HTML. The PDF is a lecture note or a similar resource.
Full Transcript
Web Application Dev. PHP & MySQL Lecturer: Ali Abdi Mohamed MSc in Computer Science And Engineering Introduction Course Objective ○ Learn to create dynamic websites and applications Technologies ○ HTML ○ PHP ○ MySQL ○ JavaScript ○ CSS...
Web Application Dev. PHP & MySQL Lecturer: Ali Abdi Mohamed MSc in Computer Science And Engineering Introduction Course Objective ○ Learn to create dynamic websites and applications Technologies ○ HTML ○ PHP ○ MySQL ○ JavaScript ○ CSS Textbook ○ Learning PHP, MySQL & JavaScript with jQuery, CSS & HTML5 6th Edition by Robin Nixon 2 Course Description PHP (stands for Hypertext Preprocessor) is a powerful tool for developing dynamic and interactive Web pages. PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. In this course you will learn about PHP, MYSQL, and how to execute scripts on your server. 3 Course Objectives To develop dynamic and attractive web pages. To understand web programming terminologies. To understand how to design forms. To understand how to create MYSQL database To understand how to use SQL statements such select, insert, update, create, delete, etc. To understand how to connect PHP pages to MYSQL database. 4 Course Outlines Chapter 1 Introduction to PHP & MySQL Chapter 2 PHP Fundamentals Chapter 3 Control Structures Chapter 4 Arrays in PHP Chapter 5 Functions & Include Files Chapter 6 Getting Data from the Client Chapter 7 Introduction to MySQL Chapter 8 Accessing MySQL Using PHP Chapter 9 PHP Cookies & Sessions Wrap up of the Course and Projects 5 HTTP and HTML HTTP is a communication standard that governs the requests and responses that take place between the browser and the web server. HTML is the standard markup language for documents designed to be displayed in a web browser. Server accepts a request from the client and attempts to reply to it in a meaningful way. ○ i.e. sending the requested web page. Client is the computer which generates the request for service. ○ Term is also applied to the web browser. There can be several other devices between client and server: ○ Routers, Proxies, Gateways, and so on. ○ They ensure that requests and responses are correctly transferred between client and server. 6 Request/Response Procedure 1. Web browser asks the web server to send it a web page. 2. Web server sends back the page. 3. Browser then takes care of displaying the page. 7 The basic client/server request/response sequence 1. You enter http://server.com into your browser’s address bar. 2. Your browser looks up the IP address for server.com. 3. Your browser issues a request for the home page at server.com. 4. The request crosses the Internet and arrives at the server.com web server. 5. The web server, having received the request, looks for the web page on its disk. 6. The web page is retrieved by 8 the server and returned to the A dynamic client/server request/response sequence 1. The web server, having received the request, fetches the home page from its hard disk. 2. The web server notices that homepage incorporates PHP scripting and passes the page to the PHP interpreter. 3. The PHP interpreter executes the PHP code. 4. Some of the PHP contains MySQL statements, which the PHP interpreter now passes to the MySQL database engine. 5. The MySQL database returns the results of the statements to the PHP interpreter. 6. The PHP interpreter returns the results of the executed PHP code, along with the results from the MySQL database, to the web server. 7. The web server returns the page to the 9 PHP - What is it? PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, and Java with a couple of unique PHP-specific features thrown in. PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. 10 When someone visits your PHP webpage, your web server processes the PHP code. It then sees which parts it needs to show to visitors (content and pictures) and hides the other stuff (file operations, math calculations, etc.) then translates your PHP into HTML. After the translation into HTML, it sends the webpage to your visitor's web browser. 11 What can PHP do? Anything. Reduce the time to create large websites. Create a customized user experience for visitors based on information that you have gathered from them. Open up thousands of possibilities for online tools. Allow creation of shopping carts for e-commerce websites. 12 Cont… PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynamic page content. But PHP can do much more. PHP can be used on all major operating systems PHP has also support for most of the web servers today. 13 Cont… Furthermore, you also have the choice of using procedural programming or object oriented programming, or a mixture of them. One of the strongest and most significant features in PHP is its support for a wide range of databases, such as MySQL, Oracle, and SQL Server. 14 What You Should Know Before starting this course it is important that you have a basic understanding and experience in the following: HTML - Know the syntax and especially HTML Forms. Basic programming knowledge. 15 What do you need? Server (Windows Apache MySQL PHP) ○ Windows (OS) ○ Apache (web-server) ○ MySQL (database) ○ PHP (language). Text editor (Notepad, Vs Code, SublimeText, etc) 16 Cont… Web browser (Google Chrome, Internet Explorer, Opera, Mozilla Firefox, etc) 17 Popular PHP Platforms Types of Websites Social Media Ecommerce Blogs, CMS Search Engine Knowledge Base Elearning Entertainment 18 Norms, rules and regulations Students should be punctual, come and leave the class on time, bring assignments on time. Mobile phones should be switched off or switched into silent mode during the class. Student will not be allowed to attend the final exam of the course if he/she is absent more than 25% of the total hours of the course. 19 Evaluation scheme Mid-term = 30% Course work = 10% Final exam = 60% 20 References Lecturer’s Note PHP Manual by Stig Sæther Bakken http://www.w3schools.com and http://www.tutorialspoint.com http://www.php.net Learning PHP, MySQL, JavaScript, CSS & HTML5, Robin Nixon, 6th edition. PHP and MySQL for Dynamic Web Sites, Larry Ullman 21 SETTING UP A DEVELOPMENT SERVER XAMPP Apache, MySQL, PHP, Perl for Windows Others: LAMP - Linux WAMP - Windows MAMP - MacIntosh XAMPP - Cross-Platform XAMPP Local web design and development environment HTML displays without need of a server Other web-based languages do need a server to operate Creates local ‘sandbox’ environment in which developers can write, deploy, and test code Setting Up a Development Server You can use WAMP (stands for Windows Apache MySQL PHP) or XAMPP (stands for X-OS Apache MySQL PHP Perl) server. WAMP works only in Windows operating system, whereas XAMPP is a cross-platform, it works in Windows, Linux, Mac etc. The key difference between them apart from OS support is security (WAMP was built with security in mind). You can download the latest XAMPP server for free from https://apachefriends.org/download.html. Install and test it probably. 26 XAMPP One problem can occur if you have VMware installed on your computer The VMware Authorization Service uses one of Apache’s ports Click on ‘Services,’ click in the Services window, and type ‘v’ Right-click on VMware Authorization Service and select ‘Stop’ This will happen if you have Vmware running on your computer, because VMw will already be bound to Port 443 This won’t cause a problem with Vmware, as long as you don’t try to launch it while XAMPP is running its Apache server NOTE: If you’re going to be developing database integrated applications, start MySQL also Testing the Installation Start the server and enter either of the following two URLs into the address bar of your browser: Localhost or 127.0.0.1 Note that the default port of XAMPP Apache server is 80. Sometimes, Other application is using that 80 port. So, When you will start XAMPP server, the XAMPP will through unexpected and notifies the server port is busy or running another application. e.g.- Skype is using 80 port to run application. 40 Testing the Installation (cont…) In this case you much change server port, for example, 8080, then restart it. You must place a colon followed by that value after either of the preceding URLs—like this: localhost:8080. You will have to do the same for all examples: localhost:8080/example.php 41 Accessing the Document Root The document root is the one that is entered when a basic URL without a path is typed into a browser, such as http://yahoo.com or, for your local server, http://localhost. By default, XAMPP uses the following location for the root directory: C:/xampp/htdocs 42 XAMPP The ‘server root’ for XAMPP is in **\xampp\htdocs (Where ‘**’ is the path to the directory where you installed XAMPP In my case, the path is D:\xampp\htdocs Testing the installation Open a new file in Notepad++ and type the above Save the file in the **\xampp\htdocs\test folder as ‘test.html’ In your browser, navigate to ‘localhost/test/test.html’ Notice that ‘htdocs’ doesn’t appear in the address. It is set as the root folder for local websites Index Page Home Page Hello world!!! 47 First Page (cont…) Save the file into the root directory (C://xampp/htdocs/test) using the filename index.php. You can now call this page up in your browser by entering the following URLs in its address bar. http://localhost/test/ You do not need to include index.php in the URL. The index page is the URL or local file that automatically loads when you write host name in the URL. 48 Using a Program Editor There are a number of good program editors available such as Notepad, Editra, Notepad++, Sublime, phpDesigner, Visual Studio Code, etc. Integrated development environments (IDEs) offer many additional features such as intellisense, in-editor debugging, program testing, as well as function descriptions, autocompletion, highlights the syntax appropriately using colors to help clarify what’s going on, and much more. We will use Visual Studio Code for this course. 49 10/8/2024 Eng. Ali Abdi Mohamed 1 FUNDAMENTAL OF PHP PHP Eng. Ali Abdi Mohamed 10/8/2024 Eng. Ali Abdi Mohamed 2 The PHP echo Statement The echo statement can output one or more strings. In general terms, the echo statement can display anything that can be displayed to the browser, such as string, numbers, variables values, the results of expressions etc. 10/8/2024 Eng. Ali Abdi Mohamed 3 Continue Since echo is a language construct not actually a function, you can use it without parentheses e.g. echo or echo(). However, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses. 10/8/2024 Eng. Ali Abdi Mohamed 4 Display Strings of Text 10/8/2024 Eng. Ali Abdi Mohamed 5 Display HTML Code 10/8/2024 Eng. Ali Abdi Mohamed 12 PHP is a Loosely Typed Language In the example above, notice that we did not have to tell PHP which data type the variable is. PHP automatically converts the variable to the correct data type, depending on its value.In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it. 10/8/2024 Eng. Ali Abdi Mohamed 13 PHP Variables Scope In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used. PHP has three different variable scopes: local global static 10/8/2024 Eng. Ali Abdi Mohamed 14 What is Global And Local Scope A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function: 10/8/2024 Eng. Ali Abdi Mohamed 15 Example 10/8/2024 Eng. Ali Abdi Mohamed 20 PHP Integer An integer is a whole number (without decimals). It is a number between - 2,147,483,648 and +2,147,483,647. 10/8/2024 Eng. Ali Abdi Mohamed 21 Rules for integers: An integer must have at least one digit (0-9) An integer cannot contain comma or blanks An integer must not have a decimal point An integer can be either positive or negative 10/8/2024 Eng. Ali Abdi Mohamed 22 String concatenation with echo You can use concatenation to join strings "end to end" while outputting them (with echo or print for example). You can concatenate variables using a. (period/dot). 10/8/2024 Eng. Ali Abdi Mohamed 23 PHP Data Types PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types: Scalar Types (predefined) Compound Types (user-defined) Special Types 10/8/2024 Eng. Ali Abdi Mohamed 24 PHP Data Types: Scalar Types It holds only single value. There are 4 scalar data types in PHP. boolean integer float string 10/8/2024 Eng. Ali Abdi Mohamed 25 PHP Data Types: Compound Types It can hold multiple values. There are 2 compound data types in PHP. array Object PHP Data Types: Special Types There are 2 special data types in PHP. resource NULL 10/8/2024 Eng. Ali Abdi Mohamed 26 Thank you CHAPTER 3 CONTROL STRUCTURES Chapter Outlines Types of Control Structures Loop Control Structures Conditional Control Structures While Loop if Statement do...while Loop if else statement for Loop if elseif statement break and continue switch Statement Statements Nested Loops 2 Types of Control Structures Sequential Control structure (default structure) Execution by which statements are executed in the same order in which they appear in the program. Conditional control structure Affect the flow of the program and execute or skip certain code according to certain criteria. Loop control structure Execute certain code a number of times according to specified criteria. 3 Conditional Control Structures Conditional control structures allows your program to take different execution paths based on decisions it makes at runtime. Conditionals alter program flow. There are three basic conditionals: PHP supports if, switch and ternary (? Operator) conditional control structures. 4 if Statements The expression in the if statement is referred to as the truth expression. If the truth expression evaluates to true, the statement or statement list following it are executed; otherwise, they are not. if (expression) statement(s) 5 if Statements Example $month = "March"; if ($a < $b) if ($month == "March") echo "$a is less than $b"; echo "It's springtime"; if ($a >= $b) $a = 2; echo "$a is greater than or equal to $b"; $b = 3; if ($a > $b) if ($a = 50) echo "Pass"; else echo "Not pass"; 8 if elseif statement (1 of 2) if (expression) statement(s) elseif (expression) statement(s) else if (expression) statement(s) else statement(s) 9 if elseif statement (2 of 2) Note that braces are required when if block contains more than one statement. The contents of the if condition can be any valid PHP expression. Also, it is a common practice by PHP developers to use else if notation instead of elseif. 10 if elseif statement Example $marks = 87; if ($marks >= 90) echo "Excellent"; elseif ($marks >= 80) echo "Very good"; elseif ($marks >= 50) echo "minimal pass"; else echo "not pass"; 11 switch Statements The switch statement is useful where one variable, or the result of an expression, can have multiple values, each of which should trigger a different activity. You can use the switch construct to replace certain lengthy if/elseif constructs. It is given an expression and compares it to all possible case expressions listed in its body. Use the break statement to end execution because a condition has been fulfilled, exit the switch statement and jump to the following statement. 12 The if...elseif...else Statement Example. A multiple-line if...elseif...else statement 12 PHP Parameterized Function PHP Parameterized functions are the functions with parameters. You can pass any number of parameters inside a 11/12/2024 function. These passed parameters act as variables inside your function. They are specified inside the parentheses, after the function Eng. Ali Abdi Mohamed name. The output depends upon the dynamic values passed as the parameters into the function. 13 PHP Parameterized Function 11/12/2024 Parameter Addition and Subtraction Example Eng. Ali Abdi Mohamed 15 PHP Parameterized Example 2 PHP Parameterized Example 2 Enter first number: Enter second number: 18 PHP Call By Value PHP allows you to call function by value and reference both. In case of PHP call by value, actual value is not modified if it 11/12/2024 is modified inside the function. PHP Call By Value 20 PHP Call By Reference In case of PHP call by reference, actual value is modified if it is modified inside the function. In such case, you need to use & 11/12/2024 (ampersand) symbol with formal arguments. The & represents reference of the variable. Let's understand the concept of call by reference by the help of Eng. Ali Abdi Mohamed examples. 21 PHP Call By Reference 22 PHP Default Argument Values Function PHP allows you to define C++ style default argument values. In such case, if you don't pass any value to the function, it will 11/12/2024 use default argument value. Let' see the simple example of using PHP default arguments in function. Eng. Ali Abdi Mohamed 23 PHP Default Argument Values Function 24 PHP Default Argument Values Function 25 PHP Variable Length Argument Function PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments in function. To do so, 11/12/2024 you need to use 3 ellipses (dots) before the argument name. The 3 dot concept is implemented for variable length argument since PHP 5.6. Eng. Ali Abdi Mohamed Let's see a simple example of PHP variable length argument function 26 PHP Variable Length Argument Function 27 PHP Recursive Function PHP also supports recursive function call like C/C++. In such case, we call current function within function. It is also known 11/12/2024 as recursion. It is recommended to avoid recursive function call over 200 recursion level because it may smash the stack and may cause Eng. Ali Abdi Mohamed the termination of script. 28 PHP Recursive Function 29 The End Eng. Ali Abdi Mohamed 11/12/2024 30 Chapter Five PHP Array Eng. Ali Abdi Mohamed 12/7/2024 1 Outline Lecture PHP Arrays PHP Indexed Array PHP Associative Array PHP Multidimensional Array PHP Array Functions Eng. Ali Abdi Mohamed 12/7/2024 2 PHP Arrays PHP array is an ordered map (contains value on the basis of key). It is used to hold multiple values of similar type in a single variable. Eng. Ali Abdi Mohamed 12/7/2024 3 Advantage of PHP Array Less Code: We don't need to define multiple variables. Easy to traverse: By the help of single loop, we can traverse all the elements of an array. Sorting: We can sort the elements of array. Eng. Ali Abdi Mohamed 12/7/2024 4 PHP Array Types There are 3 types of array in PHP. Indexed Array Associative Array Multidimensional Array Eng. Ali Abdi Mohamed 12/7/2024 5 PHP Indexed Array PHP index is represented by number which starts from 0. We can store number, string and object in the PHP array. All PHP array elements are assigned to an index number by default. Eng. Ali Abdi Mohamed 12/7/2024 6 There are two ways to define indexed array: Eng. Ali Abdi Mohamed 12/7/2024 7 There are two ways to define indexed array: Eng. Ali Abdi Mohamed 12/7/2024 8 PHP Associative Array We can associate name with each array elements in PHP using => symbol. There are two ways to define associative array: Eng. Ali Abdi Mohamed 12/7/2024 9 PHP Associative Array Eng. Ali Abdi Mohamed 12/7/2024 10 PHP Multidimensional Array PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column. Eng. Ali Abdi Mohamed 12/7/2024 11 PHP Multidimensional Array Eng. Ali Abdi Mohamed 12/7/2024 12 PHP Array Functions PHP provides various array functions to access and manipulate the elements of array. The important PHP array functions are given below. 1) PHP array() function PHP array() function creates and returns an array. It allows you to create indexed, associative and multidimensional arrays. Eng. Ali Abdi Mohamed 12/7/2024 13 PHP Array Functions Eng. Ali Abdi Mohamed 12/7/2024 14 PHP Array Functions 2) PHP array_change_key_case() function PHP array_change_key_case() function changes the case of all key of an array. Eng. Ali Abdi Mohamed 12/7/2024 15 PHP Array Functions Eng. Ali Abdi Mohamed 12/7/2024 16 PHP count() function PHP count() function counts all elements in an array. Eng. Ali Abdi Mohamed 12/7/2024 17 PHP sort() function Eng. Ali Abdi Mohamed 12/7/2024 18 PHP array_search() function PHP array_search() function searches the specified value in an array. It returns key if search is successful. Eng. Ali Abdi Mohamed 12/7/2024 19 The End Eng. Ali Abdi Mohamed 12/7/2024 20