Introduction to Web programming

Slides:



Advertisements
Similar presentations
PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Advertisements

INTERNET APPLICATION DEVELOPMENT For More visit:
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
INTERNET APPLICATION DEVELOPMENT PRACTICAL ON CONNECTING TO MYSQL.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
PHP Constructs Advance Database Management Systems Lab no.3.
Introduction to PHP.
Outline if...else...elseif Statements Switch Loops Functions Arrays Forms.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
PHP. PHP User Defined Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
PHP: MySQL. PHP Connect to MySQL PHP 5 and later can work with a MySQL database using: – MySQLi extension (the "i" stands for improved) – PDO (PHP Data.
PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.
CHAPTER 10 PHP MySQL Database
CSC 2720 Building Web Applications Accessing MySQL from PHP.
MySQL MySQL and PHP – interacting with a database.
INLS 623 – D ATABASE A PPLICATION D EVELOPMENT AND I NTERNET A PPLICATIONS Instructor: Jason Carter.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
A pache M ySQL P hp Robert Mudge Reference:
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
Web Systems & Technologies
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
PHP Built-In Functions
PHP: MySQL Lecture 14 Kanida Sinmai
>> Fundamental Concepts in PHP
Introduction to Dynamic Web Programming
Functions A function is a block of code with a name. function functionName() { code to be executed; }
IS1500: Introduction to Web Development
Web Database Programming Using PHP
>> PHP: Arrays.
CHAPTER 5 SERVER SIDE SCRIPTING
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
Web Design and Development
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Introduction to Web programming
Introduction to Web programming
Ch. 3. PHP (이 강의 내용의 대부분 예들은 w3schools. com/php/default
ISC440: Web Programming 2 Server-side Scripting PHP 3
MySQL Web Application Connecting to a MySQL database
PHP Function.
PHP.
JavaScript Arrays.
MCS/BCS.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Basics (Cont...).
Tutorial 6 PHP & MySQL Li Xu
MySQL Web Application Connecting to a MySQL database
PHP an introduction.
PHP-II.
Introduction to Web programming
Conection
PHP Array.
Introduction to Web programming
PHP Programming Using Cloud 9 IDE.
PHP By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Introduction to Web programming 0731213

Lecture 1 PHP functions

PHP User Defined Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in a program. A function will not execute immediately when a page loads. A function will be executed by a call to the function.

Create a User Defined Function in PHP A user defined function declaration starts with the word "function": Function names are NOT case-sensitive. Note: A function name can start with a letter or underscore (not a number). Tip: Give the function a name that reflects what the function does! function functionName() { code to be executed; }

Create a User Defined Function in PHP In the example below, we create a function named "writeMsg()". The opening curly brace ( { ) indicates the beginning of the function code. The closing curly brace ( } ) indicates the end of the function. The function outputs "Hello world!". To call the function, just write its name: <?php function writeMsg() { echo "Hello world!"; } writeMsg(); // call the function ?>

Create a User Defined Function in PHP In the example below, we create a function named "connect()". The function connects to the server and database. <?php connect(); function connect(){ $servername = "localhost"; $username = "root"; $password = ""; $dbname = "mydb"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>

PHP Function Arguments Information can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

PHP Function Arguments The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name: <?php function familyName($fname) { echo "$fname Refsnes.<br>"; } familyName("Jani"); familyName("Hege"); familyName("Kai Jim"); familyName("Borge"); ?>

PHP Function Arguments The following example has a function with two arguments ($fname and $year): <?php function familyName($fname, $year) { echo "$fname Refsnes. Born in $year <br>"; } familyName("Hege", "1975"); familyName("Stale", "1978"); familyName("Kai Jim", "1983"); ?>

Create a User Defined Function in PHP In the example below, we create a function named "printGuests()". The function print all the rows from table MyGuests. printGuests($conn); function printGuests($conn){ $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo "id: $row[id] - Name: $row[firstname] $row[lastname] <br>"; } } else { echo "0 results";

PHP Default Argument Value The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument: <?php function setHeight($minheight = 50) { echo "The height is : $minheight <br>"; } setHeight(350); setHeight(); // will use the default value of 50 setHeight(135); setHeight(80); ?>

PHP Functions - Returning values To let a function return a value, use the return statement:  <?php function sum($x, $y) { $z = $x + $y; return $z; } echo "5 + 10 = " . sum(5, 10) . "<br>"; echo "7 + 13 = " . sum(7, 13) . "<br>"; echo "2 + 4 = " . sum(2, 4); ?>

PHP Functions - Returning values In the example below, we modify the function "connect()“ to return the connection. <?php $conn = connect(); function connect(){ $servername = "localhost"; $username = "root"; $password = ""; $dbname = "mydb"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully<br/>"; return $conn; ?>

Complete Example

Lecture 2 PHP Arrays

What is an Array? An array is a special variable, which can hold more than one value at a time. An array can hold many values under a single name, and you can access the values by referring to an index number. An array stores multiple values in one single variable

Create an Array in PHP In PHP, the array() function is used to create an array: In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index Associative arrays - Arrays with named keys Multidimensional arrays - Arrays containing one or more arrays array();

PHP Indexed Arrays There are two ways to create indexed arrays: The index can be assigned automatically (index always starts at 0), like this: or the index can be assigned manually: $cars = array("Volvo", "BMW", "Toyota"); $cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota";

PHP Indexed Arrays The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values: <?php $cars = array("Volvo", "BMW", "Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>

Get The Length of an Array - The count() Function The count() function is used to return the length (the number of elements) of an array: <?php $cars = array("Volvo", "BMW", "Toyota"); echo count($cars); ?>

Loop Through an Indexed Array To loop through and print all the values of an indexed array, you could use a for loop, like this: <?php $cars = array("Volvo", "BMW", "Toyota"); $arrlength = count($cars); for($x = 0; $x < $arrlength; $x++) { echo $cars[$x]; echo "<br>"; } ?>

Self Study Associative arrays (https://www.w3schools.com/php/php_arrays.asp) Multidimensional arrays (https://www.w3schools.com/php/php_arrays_multi.asp) PHP 5 Array Functions (https://www.w3schools.com/php/php_ref_array.asp) PHP 5 Sorting Arrays (https://www.w3schools.com/php/php_arrays_sort.asp)

References https://www.w3schools.com/ Robin Nixon, Learning PHP, MySQL, JavaScript, and CSS, 2013 Mike McGrath, PHP & My SQL in easy steps, 2012.

The End