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  PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)  PHP is an open source software (OSS)  PHP is free to download and use
What is a PHP File?  PHP files may contain text, HTML tags and scripts  PHP files are returned to the browser as plain HTML  PHP files have a file extension of ".php", ".php3", or ".phtml"
What is MySQL?  MySQL is a small database server  MySQL is ideal for small and medium applications  MySQL supports standard SQL  MySQL compiles on a number of platforms  MySQL is free to download and use
+  PHP combined with MySQL are cross- platform (means that you can develop in Windows and serve on a Unix platform)
Why PHP?  PHP runs on different platforms (Windows, Linux, Unix, etc.)  PHP is compatible with almost all web servers used today (Apache, IIS, etc.)  PHP is FREE to download from the official PHP resource: www.php.net  PHP is easy to learn and runs efficiently on the server side
What do You Need?  PHP Interpreter Download PHP for free here: http://www.php.net/downloads.php  MySQL Database Download MySQL for free here: http://www.mysql.com/downloads/index.html  Apache Server Download Apache for free here: http://httpd.apache.org/download.cgi
Where to Start?  Install an Apache server on a Windows or Linux machine  Install PHP on a Windows or Linux machine  Install MySQL on a Windows or Linux machine
The Process Web Server P H P Interpreter PHP File Web Browser MySQL Database Page Request Obtain Data Dynamic Page PHP retrieves MySQL data to produce Dynamic Web Pages
Basic PHP Syntax  A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.  We have an example of a simple PHP script which sends the text "Hello World" to the browser:
The Hello World <html> <body> <?php echo("Hello World!“); ?> </body> </html>
Basic PHP Syntax  A PHP scripting block always starts with <?php and ends with ?>.  A PHP scripting block can be placed anywhere in the HTML document.  Each code line in PHP must end with a semicolon.  The semicolon is a separator and is used to distinguish one set of instructions from another.
Variables in PHP  All variables in PHP start with a $ sign symbol.  Variables may contain strings, numbers, or arrays. <html> <body> <?php $txt="Hello World"; echo($txt); ?> </body> </html>
Concatenation  To concatenate two or more variables together, use the dot (.) operator <html> <body> <?php $first_name=“Juan"; $last_name=“Dela Cruz"; echo $first_name . " " . $last_name ; ?> </body> </html>
Comments in PHP  In PHP, we use // to make a single-line comment or /* and */ to make a large comment block. <html> <body> <?php //This is a single line comment /* This is a comment block */ ?> </body> </html>
PHP Operators: Arithmetic Operator Description Example Result + Addition x=2 x+2 4 - Subtraction x=2 5-x 3 * Multiplication x=4 x*5 20 / Division 15/5 5/2 3 2.5 % Modulus (division remainder) 5%2 10%8 10%2 1 2 0 ++ Increment x=5 x++ x=6 -- Decrement x=5 x-- x=4
PHP Operators: Assignment Operator Example Is The Same As = x=y x=y += x+=y x=x+y -= x-=y x=x-y *= x*=y x=x*y /= x/=y x=x/y %= x%=y x=x%y
PHP Operators: Comparison Operator Description Example == is equal to 5==8 returns false != is not equal 5!=8 returns true > is greater than 5>8 returns false < is less than 5<8 returns true >= is greater than or equal to 5>=8 returns false <= is less than or equal to 5<=8 returns true
PHP Operators: Logical Operator Description Example && and x=6 y=3 (x < 10 && y > 1) returns true || or x=6 y=3 (x==5 || y==5) returns false ! not x=6 y=3 !(x==y) returns true
Control Structures  Conditional Statements The IF (…ELSE) statement The SWITCH statement  Looping Statements The WHILE statement The DO…WHILE statement The FOR statement
The IF Statement Syntax: if (condition){ code to be executed if condition is true; } Else{ code to be executed if condition is false; }
Example: IF Statement <html> <body> <?php $d=date("D"); if ($d=="Fri"){ echo "Have a nice weekend!"; }else{ echo "Have a nice day!"; ?> </body> </html>
The SWITCH Statement Syntax: switch (expression){ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
Example: SWITCH Statement <html> <body> <?php switch ($x){ case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; } ?> </body> </html>
The WHILE Statement  The while statement will execute a block of code if and as long as a condition is true.  Syntax: while (condition){ code block to be executed; }
Example: WHILE Statement <html> <body> <?php $i=1; while($i<=5){ echo "The number is " . $i . "<br/>"; $i++; } ?> </body> </html>
The do...while Statement  The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.  Syntax: do { code block to be executed; }while (condition);
Example: DO…WHILE Statement <html> <body> <?php $i=0; do{ $i++; echo "The number is " . $i . "<br/>"; }while ($i<5); ?> </body> </html>
The FOR Statement  The for statement is used when you know how many times you want to execute a statement or a list of statements.  Syntax for (initialization; condition; increment){ code to be executed; }
Example: FOR Statement <html> <body> <?php for ($i=1; $i<=5; $i++){ echo "Hello World!<br/>"; } ?> </body> </html>
PHP Functions  The general syntax form of a function: <?php function functionname($arg1,$arg2,.., $argN) { statements; } ?>
PHP Built-in Functions  The phpinfo() function is used to output PHP information.  This function is useful for trouble shooting, providing the version of PHP, and how it is configured.  PHP provides many functions for ease in development, consult http://www.php.net for the list.
PHP Form Handling  The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.
An HTML Form <html> <body> <form action="welcome.php" method="POST"> Enter your name:<input type="text" name="name"> Enter your age: <input type="text" name="age"> <input type="submit"> </form> </body> </html>
The welcome.php File <html> <body> Welcome <?php echo $_POST[‘name’]; ?> <br>You are <?php echo $_POST[‘age’]; ?> years old! </body> </html>
Form Methods – GET & POST  The GET Method PHP stores all the "posted" values into an associative array called "$_POST".  The POST Method  PHP passes the variables along to the web page’s URL by appending them onto the end of the URL
PHP and MySQL  Setup your database in MySQL  Create a connection from your PHP file to the database server  Select the database to be manipulated on the database server  Using SQL Query Statements, data may be retrieved, added, deleted, updated.
PHP Functions for MySQL  mysql_connect() Open a connection to a MySQL Server Syntax: mysql_connect(h_name, u_name, pswrd) Example: $link=mysql_connect(‘localhost’, ‘root’, ‘one’)
PHP Functions for MySQL  mysql_select_db() Select a MySQL database to manipulate Syntax: mysql_select_db(database_name,connectio n); Example: $select_db=mysql_select_db(‘sample’,$db conn);
PHP Functions for MySQL  mysql_query() Send a MySQL query to the database server for execution Syntax: mysql_query(query); Example: $query=‘SELECT * FROM tblStudents’; $result=mysql_query($query);
PHP Functions for MySQL  mysql_fetch_array() Fetch a result row as an associative array, a numeric array, or both Syntax:  mysql_fetch_array(query); Example: $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result)) { printf("ID: “ . $row[‘id’] Name: “ . $row[‘name’]); }
SQL Query Statements  Retrieving records: SELECT SELECT * FROM <table_name> WHERE <condition>  Adding Records: INSERT INSERT INTO <table_name> VALUES(value1, value2,…)
SQL Query Statements  Deleting Records: DELETE DELETE <table_name> WHERE <condition>  Updating Records: UPDATE UPDATE <table_name> SET field1=new_value1, field2=new_value2, … WHERE <condition>
What is a Cookie?  A cookie is often used to identify a user.  A cookie is a small file that the server embeds on the user's computer.  Each time the same computer requests for a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
How to Create a Cookie  The setcookie() function is used to create cookies. Note: The setcookie() function must appear BEFORE the <html> tag.  Syntax setcookie(name, value, expire, path, domain);
Example: Setting a Cookie <?php setcookie("uname", $name, time()+36000); ?> <html> <body> <p>A cookie was set on this page! The cookie will be active when the client has sent the cookie back to the server.</p> </body> </html>
How to Retrieve a Cookie Value  When a cookie is set, PHP uses the cookie name as a variable.  To access a cookie you just refer to the cookie name as a variable. Tip: Use the isset() function to find out if a cookie has been set.
Example: Retrieving a Cookie <html> <body> <?php if (isset($_COOKIE["uname"])){ echo "Welcome " . $_COOKIE["uname"] . "!<br>"; }Else{ echo "You are not logged in!<br>"; } ?> </body> </html>
The END THANK YOU!!!

PHP and MySQL.ppt

  • 2.
    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  PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)  PHP is an open source software (OSS)  PHP is free to download and use
  • 3.
    What is aPHP File?  PHP files may contain text, HTML tags and scripts  PHP files are returned to the browser as plain HTML  PHP files have a file extension of ".php", ".php3", or ".phtml"
  • 4.
    What is MySQL? MySQL is a small database server  MySQL is ideal for small and medium applications  MySQL supports standard SQL  MySQL compiles on a number of platforms  MySQL is free to download and use
  • 5.
    +  PHP combinedwith MySQL are cross- platform (means that you can develop in Windows and serve on a Unix platform)
  • 6.
    Why PHP?  PHPruns on different platforms (Windows, Linux, Unix, etc.)  PHP is compatible with almost all web servers used today (Apache, IIS, etc.)  PHP is FREE to download from the official PHP resource: www.php.net  PHP is easy to learn and runs efficiently on the server side
  • 7.
    What do YouNeed?  PHP Interpreter Download PHP for free here: http://www.php.net/downloads.php  MySQL Database Download MySQL for free here: http://www.mysql.com/downloads/index.html  Apache Server Download Apache for free here: http://httpd.apache.org/download.cgi
  • 8.
    Where to Start? Install an Apache server on a Windows or Linux machine  Install PHP on a Windows or Linux machine  Install MySQL on a Windows or Linux machine
  • 9.
    The Process Web Server PH P Interpreter PHP File Web Browser MySQL Database Page Request Obtain Data Dynamic Page PHP retrieves MySQL data to produce Dynamic Web Pages
  • 10.
    Basic PHP Syntax A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.  We have an example of a simple PHP script which sends the text "Hello World" to the browser:
  • 11.
  • 12.
    Basic PHP Syntax A PHP scripting block always starts with <?php and ends with ?>.  A PHP scripting block can be placed anywhere in the HTML document.  Each code line in PHP must end with a semicolon.  The semicolon is a separator and is used to distinguish one set of instructions from another.
  • 13.
    Variables in PHP All variables in PHP start with a $ sign symbol.  Variables may contain strings, numbers, or arrays. <html> <body> <?php $txt="Hello World"; echo($txt); ?> </body> </html>
  • 14.
    Concatenation  To concatenatetwo or more variables together, use the dot (.) operator <html> <body> <?php $first_name=“Juan"; $last_name=“Dela Cruz"; echo $first_name . " " . $last_name ; ?> </body> </html>
  • 15.
    Comments in PHP In PHP, we use // to make a single-line comment or /* and */ to make a large comment block. <html> <body> <?php //This is a single line comment /* This is a comment block */ ?> </body> </html>
  • 16.
    PHP Operators: Arithmetic OperatorDescription Example Result + Addition x=2 x+2 4 - Subtraction x=2 5-x 3 * Multiplication x=4 x*5 20 / Division 15/5 5/2 3 2.5 % Modulus (division remainder) 5%2 10%8 10%2 1 2 0 ++ Increment x=5 x++ x=6 -- Decrement x=5 x-- x=4
  • 17.
    PHP Operators: Assignment OperatorExample Is The Same As = x=y x=y += x+=y x=x+y -= x-=y x=x-y *= x*=y x=x*y /= x/=y x=x/y %= x%=y x=x%y
  • 18.
    PHP Operators: Comparison OperatorDescription Example == is equal to 5==8 returns false != is not equal 5!=8 returns true > is greater than 5>8 returns false < is less than 5<8 returns true >= is greater than or equal to 5>=8 returns false <= is less than or equal to 5<=8 returns true
  • 19.
    PHP Operators: Logical OperatorDescription Example && and x=6 y=3 (x < 10 && y > 1) returns true || or x=6 y=3 (x==5 || y==5) returns false ! not x=6 y=3 !(x==y) returns true
  • 20.
    Control Structures  ConditionalStatements The IF (…ELSE) statement The SWITCH statement  Looping Statements The WHILE statement The DO…WHILE statement The FOR statement
  • 21.
    The IF Statement Syntax: if(condition){ code to be executed if condition is true; } Else{ code to be executed if condition is false; }
  • 22.
    Example: IF Statement <html> <body> <?php $d=date("D"); if($d=="Fri"){ echo "Have a nice weekend!"; }else{ echo "Have a nice day!"; ?> </body> </html>
  • 23.
    The SWITCH Statement Syntax: switch(expression){ case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }
  • 24.
    Example: SWITCH Statement <html> <body> <?php switch($x){ case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; } ?> </body> </html>
  • 25.
    The WHILE Statement The while statement will execute a block of code if and as long as a condition is true.  Syntax: while (condition){ code block to be executed; }
  • 26.
    Example: WHILE Statement <html> <body> <?php $i=1; while($i<=5){ echo"The number is " . $i . "<br/>"; $i++; } ?> </body> </html>
  • 27.
    The do...while Statement The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.  Syntax: do { code block to be executed; }while (condition);
  • 28.
    Example: DO…WHILE Statement <html> <body> <?php $i=0; do{ $i++; echo"The number is " . $i . "<br/>"; }while ($i<5); ?> </body> </html>
  • 29.
    The FOR Statement The for statement is used when you know how many times you want to execute a statement or a list of statements.  Syntax for (initialization; condition; increment){ code to be executed; }
  • 30.
    Example: FOR Statement <html> <body> <?php for($i=1; $i<=5; $i++){ echo "Hello World!<br/>"; } ?> </body> </html>
  • 31.
    PHP Functions  Thegeneral syntax form of a function: <?php function functionname($arg1,$arg2,.., $argN) { statements; } ?>
  • 32.
    PHP Built-in Functions The phpinfo() function is used to output PHP information.  This function is useful for trouble shooting, providing the version of PHP, and how it is configured.  PHP provides many functions for ease in development, consult http://www.php.net for the list.
  • 33.
    PHP Form Handling The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.
  • 34.
    An HTML Form <html> <body> <formaction="welcome.php" method="POST"> Enter your name:<input type="text" name="name"> Enter your age: <input type="text" name="age"> <input type="submit"> </form> </body> </html>
  • 35.
    The welcome.php File <html> <body> Welcome <?php echo$_POST[‘name’]; ?> <br>You are <?php echo $_POST[‘age’]; ?> years old! </body> </html>
  • 36.
    Form Methods –GET & POST  The GET Method PHP stores all the "posted" values into an associative array called "$_POST".  The POST Method  PHP passes the variables along to the web page’s URL by appending them onto the end of the URL
  • 37.
    PHP and MySQL Setup your database in MySQL  Create a connection from your PHP file to the database server  Select the database to be manipulated on the database server  Using SQL Query Statements, data may be retrieved, added, deleted, updated.
  • 38.
    PHP Functions forMySQL  mysql_connect() Open a connection to a MySQL Server Syntax: mysql_connect(h_name, u_name, pswrd) Example: $link=mysql_connect(‘localhost’, ‘root’, ‘one’)
  • 39.
    PHP Functions forMySQL  mysql_select_db() Select a MySQL database to manipulate Syntax: mysql_select_db(database_name,connectio n); Example: $select_db=mysql_select_db(‘sample’,$db conn);
  • 40.
    PHP Functions forMySQL  mysql_query() Send a MySQL query to the database server for execution Syntax: mysql_query(query); Example: $query=‘SELECT * FROM tblStudents’; $result=mysql_query($query);
  • 41.
    PHP Functions forMySQL  mysql_fetch_array() Fetch a result row as an associative array, a numeric array, or both Syntax:  mysql_fetch_array(query); Example: $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result)) { printf("ID: “ . $row[‘id’] Name: “ . $row[‘name’]); }
  • 42.
    SQL Query Statements Retrieving records: SELECT SELECT * FROM <table_name> WHERE <condition>  Adding Records: INSERT INSERT INTO <table_name> VALUES(value1, value2,…)
  • 43.
    SQL Query Statements Deleting Records: DELETE DELETE <table_name> WHERE <condition>  Updating Records: UPDATE UPDATE <table_name> SET field1=new_value1, field2=new_value2, … WHERE <condition>
  • 44.
    What is aCookie?  A cookie is often used to identify a user.  A cookie is a small file that the server embeds on the user's computer.  Each time the same computer requests for a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.
  • 45.
    How to Createa Cookie  The setcookie() function is used to create cookies. Note: The setcookie() function must appear BEFORE the <html> tag.  Syntax setcookie(name, value, expire, path, domain);
  • 46.
    Example: Setting aCookie <?php setcookie("uname", $name, time()+36000); ?> <html> <body> <p>A cookie was set on this page! The cookie will be active when the client has sent the cookie back to the server.</p> </body> </html>
  • 47.
    How to Retrievea Cookie Value  When a cookie is set, PHP uses the cookie name as a variable.  To access a cookie you just refer to the cookie name as a variable. Tip: Use the isset() function to find out if a cookie has been set.
  • 48.
    Example: Retrieving aCookie <html> <body> <?php if (isset($_COOKIE["uname"])){ echo "Welcome " . $_COOKIE["uname"] . "!<br>"; }Else{ echo "You are not logged in!<br>"; } ?> </body> </html>
  • 49.