Tutorial 6 PHP My SQL Li Xu Email

  • Slides: 18
Download presentation
Tutorial 6 PHP & My. SQL Li Xu Email: xuli@se. cuhk. edu. hk Department

Tutorial 6 PHP & My. SQL Li Xu Email: xuli@se. cuhk. edu. hk Department of Systems Engineering and Engineering Management 1

Outline • PHP: Hypertext Preprocessor • PHP Introduction • PHP Syntax • PHP Variables

Outline • PHP: Hypertext Preprocessor • PHP Introduction • PHP Syntax • PHP Variables • PHP Functions • My. SQL Database • My. SQL Introduction • My. SQL Connect • My. SQL Create DB • My. SQL Operations 2

PHP Introduction • What is PHP? Ø Ø an acronym for “PHP: Hypertext Preprocessor”

PHP Introduction • What is PHP? Ø Ø an acronym for “PHP: Hypertext Preprocessor” a widely-used, open source scripting language PHP scripts are executed on the server free to download and use • What is a PHP File? Ø PHP files can contain text, HTML, CSS, Java. Script, and PHP code Ø PHP code are executed on the server, and the result is returned to the browser as plain HTML Ø PHP files have extension ". php" 3

PHP Introduction • What can PHP do? Ø Ø Ø Ø generate dynamic page

PHP Introduction • What can PHP do? Ø Ø Ø Ø generate dynamic page content create, open, read, write, delete, and close files on the server collect form data send and receive cookies add, delete, modify data in your database be used to control user-access encrypt data • Why PHP? Ø run on various platforms (Windows, Linux, Unix, etc. ) Ø compatible with almost all servers used today (Apache, IIS, etc. ) Ø supports a wide range of databases 4

PHP Syntax • Basic PHP Syntax Ø A PHP script can be placed anywhere

PHP Syntax • Basic PHP Syntax Ø A PHP script can be placed anywhere in the docment, and starts with ‘<? php’ and ends with ‘? >’ Ø The default file extension for PHP files is “. php” • Comments in PHP 5

PHP Syntax • PHP Case Sensitivity Ø In PHP, all keywords (e. g. if,

PHP Syntax • PHP Case Sensitivity Ø In PHP, all keywords (e. g. if, else, while, echo, etc. ), classes, functions, and user-defined functions are NOT casesensitive. Ø However, all variable names are case-sensitive 6

PHP Variables • Creating (Declaring) PHP Variables Ø In PHP, a variable starts with

PHP Variables • Creating (Declaring) PHP Variables Ø In PHP, a variable starts with the $ sign, followed by the name of the variable: Ø PHP has no command for declaring a variable, it is created the moment you first assign a value to it. Ø A variable name must start with a letter or the underscore character, cannot start with a number. Ø A variable name can only contain alpha-numeric characters and underscores (A-z, 0 -9, and _ ) Ø Variable names are case-sensitive ($age and $AGE are 7

PHP Variables • Output Variables Ø The PHP echo statement is often used to

PHP Variables • Output Variables Ø The PHP echo statement is often used to output data to the screen: • PHP is a loosely typed language Ø PHP automatically converts the variable to the correct data type, depending on its value 8

PHP Functions • Create a user defined function in PHP Ø A user-defined function

PHP Functions • Create a user defined function in PHP Ø A user-defined function declaration starts with the word function: • PHP function arguments Ø 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. 9

PHP Functions • PHP default argument value • Returning values 10

PHP Functions • PHP default argument value • Returning values 10

My. SQL Introduction • What is My. SQL? Ø Ø Ø a database system

My. SQL Introduction • What is My. SQL? Ø Ø Ø a database system used on the web, runs on a server ideal for both small and large applications fast, reliable, and easy to use standard SQL complies on a number of platforms free to download and use • Download My. SQL Database Ø If you don’t have a PHP server with a My. SQL Database, you can download it for free via http: //www. mysql. com 11

My. SQL Connect My. SQLi Object-Oriented • PHP connect to My. SQL Ø PHP

My. SQL Connect My. SQLi Object-Oriented • PHP connect to My. SQL Ø PHP 5 and later can work with a My. SQL database using: ü My. SQLi extension (the “i” stands for imporved): http: //php. net/manual/en/mysqli. installation. php ü PDO (PHP Data Objects) http: //php. net/manual/en/pdo. installation. php • Open a connection to My. SQL Ø Before we can access data in PDO the My. SQL database, we need to be able to connect to the server 12

My. SQL Connect • PHP connect to My. SQL Ø Close the connection ü

My. SQL Connect • PHP connect to My. SQL Ø Close the connection ü My. SQLi Object-Oriented: $conn->close(); ü PDO: $conn = null; 13

My. SQL Create DB • A database consists of one or more tables, you

My. SQL Create DB • A database consists of one or more tables, you can use special CREATE privileges to create a My. SQL database: Ø Create a My. SQL Database using My. SQLi Ø Create a My. SQL Database using PDO 14

My. SQL Operations (SQL) • Create a table: • Select data: Ø select data

My. SQL Operations (SQL) • Create a table: • Select data: Ø select data from one or more tables: SELECT column_name(s) FROM table_name • Insert data: Ø select all columns from a table: SELECT * FROM table_name 15

My. SQL Operations (SQL) • Delete data from My. SQL: e. g. : delete

My. SQL Operations (SQL) • Delete data from My. SQL: e. g. : delete from My. Guests where id=3 • Update data: • e. g. : update My. Guests set lastname=‘Doe’ where id=2 16

Resources PHP: https: //www. w 3 schools. com/php/default. asp • My. SQL: https: //www.

Resources PHP: https: //www. w 3 schools. com/php/default. asp • My. SQL: https: //www. w 3 schools. com/php_mysql_intro. asp • 17

Questions and Answers 18

Questions and Answers 18