Open Source Package PHP & MySQL


Published on

Web Development on PHP/MySQL

Published in: Education, Technology
1 Comment
  • superb material to refer.........Thankkkkkkkkkkkkkkssssssssssssss................
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Open Source Package PHP & MySQL

  1. 1. PHP and MySQL
  2. 2. PHP Language <ul><li>A recursive acronym: P HP H ypertext P reprocessor </li></ul><ul><li>A scripting language designed for creating dynamic and data-driven Web pages </li></ul><ul><li>PHP is a server-side scripting language; it is parsed and interpreted on the server side of a Web application and the resulting output is sent to the browser. (VBScript and JavaScript are mostly client-side). </li></ul><ul><li>Designed to work with the MySQL database system, but can also connect to other database systems. </li></ul>
  3. 3. PHP Language <ul><li>PHP and MySQL are open-source; they can be obtained free-of-charge and the source codes are available for development. </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><li>You will need to install PHP to work with your MS IIS or Apache Web server. </li></ul><ul><li>PHP and MySQL are becoming increasingly popular in recent years because it is free of charge. Other software packages like Microsoft ASP.NET are expensive for small and medium enterprises. </li></ul>
  4. 4. A Simple PHP Example <ul><ul><li><HTML> </li></ul></ul><ul><ul><li><HEAD> </li></ul></ul><ul><ul><li><TITLE>PHP Example</TITLE> </li></ul></ul><ul><ul><li></HEAD> </li></ul></ul><ul><ul><li><BODY> <? </li></ul></ul><ul><ul><li>echo &quot;<b>Welcome</b>&quot;; // print the string here </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li></BODY> </HTML> </li></ul></ul><ul><li>PHP scripts are enclosed by the <?php And ?> tags. </li></ul><ul><ul><li>Can simply use <? for the opening tag. </li></ul></ul><ul><li>All PHP statements end with a semicolon (unless it ends the closing tag on the same line) ‏ </li></ul><ul><li>Comments can be marked by # , // or /* */ </li></ul>
  5. 5. Including Files <ul><li>You can include common files (like header, footer, and navigation bars) in PHP. </li></ul><ul><ul><li><? include(&quot;;) ?> </li></ul></ul>
  6. 6. Variables in PHP <ul><li>Variable names start with the dollar sign $ </li></ul><ul><li>You can use letters and underscore for variable names. </li></ul><ul><li>The first character after $ cannot be a number </li></ul><ul><li>Variable names are case-sensitive </li></ul><ul><li>For example: </li></ul><ul><ul><li><? $name = &quot;Michael&quot;; ?> </li></ul></ul><ul><ul><li><? $i = 1; ?> </li></ul></ul>
  7. 7. Variables in PHP <ul><li>The three main types of variables in PHP: </li></ul><ul><ul><li>Scalar </li></ul></ul><ul><ul><li>Array </li></ul></ul><ul><ul><li>Object </li></ul></ul><ul><li>Scalar can be Integer, Double, Boolean, or String </li></ul><ul><li>When you assign a value to a variable, its data type is also assigned. </li></ul>
  8. 8. Array <ul><li>Arrays can be created with integers or strings as keys. </li></ul><ul><li><?php $arr = array(&quot;UK&quot; => &quot;London&quot;, 12 => 56); echo $arr[&quot;UK&quot;]; // London echo $arr[12];    // 56 ?> </li></ul>
  9. 9. Object <ul><li>Use the class statement to define a class. </li></ul><ul><li>Use the new statement to create an object. </li></ul><ul><li><?php class test {    function do_something()    {        echo &quot;Do something&quot;;    } } $my_test = new test; $my_test->do_something(); ?> </li></ul>
  10. 10. Basic Operators <ul><li>Assignment/Arithmetic operators </li></ul><ul><ul><li>= + - * / % </li></ul></ul><ul><ul><li>++ -- += -= *= /= </li></ul></ul><ul><li>Comparison operators </li></ul><ul><ul><li>== (equal value) ‏ </li></ul></ul><ul><ul><li>=== (identical value and data type) ‏ </li></ul></ul><ul><ul><li>!= or <> </li></ul></ul><ul><ul><li>< > <= >= </li></ul></ul><ul><li>Logical operators </li></ul><ul><ul><li>! && || </li></ul></ul>
  11. 11. String Concatenation <ul><li>Same as Perl </li></ul><ul><li>. concatenate strings </li></ul><ul><li>.= concatenate and assign </li></ul><ul><li>Example: </li></ul><ul><li>$word1 = &quot;Play&quot;; </li></ul><ul><li>$full_string = $word1 . &quot;Station&quot;; </li></ul>
  12. 12. String Functions <ul><li>There are a lot of string functions in PHP that you can use. For example: </li></ul><ul><ul><li>int strlen(string str) ‏ </li></ul></ul><ul><ul><li>string strtoupper(string str) ‏ </li></ul></ul><ul><ul><li>string strtolower(string str) ‏ </li></ul></ul><ul><ul><li>int strcmp(string str1, string str2) ‏ </li></ul></ul><ul><ul><li>int strcasecmp(string str1, string str2) ‏ </li></ul></ul><ul><ul><li>string strstr(string src, string target) ‏ </li></ul></ul><ul><ul><li>int strpos(string src, string target [, int offset]) ‏ </li></ul></ul><ul><li>You can find them in the PHP manual: </li></ul>
  13. 13. Conditions <ul><li>boolean variable: TRUE(1) or FALSE(0) ‏ </li></ul><ul><li>if (EXPR) { STATEMENTS; } </li></ul><ul><li>elseif (EXPR) { STATEMENTS; } </li></ul><ul><li>else { STATEMENTS; } </li></ul><ul><li>switch-case </li></ul>
  14. 14. Loops <ul><li>while ( EXPR ) </li></ul><ul><li>{ STATEMENTS; } </li></ul><ul><li>do { STATEMENTS; </li></ul><ul><li>} while (EXPR); </li></ul><ul><li>for ( INIT_EXPR; COND_EXPR; LOOP_EXPR ) </li></ul><ul><li>{ STATEMENTS; } </li></ul><ul><li>foreach (ARRAY as VARIABLE) </li></ul><ul><li>{ STATEMENTS; } </li></ul><ul><li>break, continue </li></ul>
  15. 15. Alternative Syntax <ul><li>It is possible to write control statements and loops using the following “colon” format: </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>if ($a == 5): echo &quot;A is equal to 5&quot;; endif; </li></ul></ul><ul><ul><li>?> </li></ul></ul>
  16. 16. User-defined Functions <ul><li>Functions can be defined using the function keyword. </li></ul><ul><li><?php function function_name($arg_1, $arg_2, ...) {    statements;    return $some_value; // optional </li></ul><ul><li>} ?> </li></ul>
  17. 17. Mixing HTML and PHP Codes <ul><li>PHP codes can be easily inserted anywhere in an HTML page. </li></ul><ul><li>You can even mix the codes together, usually to avoid writing too many “echo” statements with escape characters. </li></ul><ul><ul><li><? </li></ul></ul><ul><ul><li>if ($i == 1) { </li></ul></ul><ul><ul><li>?> <h2>The condition is true</h2> <center><b>$i is 1</b></center> <? </li></ul></ul><ul><ul><li>} else { </li></ul></ul><ul><ul><li>?> <h2>The condition is false</h2> <center><b>$i is not 1</b></center> <? </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>?> </li></ul></ul>HTML PHP
  18. 18. Shortcut for Writing Echos <ul><li>Instead of writing </li></ul><ul><ul><li><? echo expression ?> </li></ul></ul><ul><li>You can use the following shortcut </li></ul><ul><ul><li><?= expression ?> </li></ul></ul><ul><li>This is useful for inserting expressions and variables quickly into HTML pages. </li></ul>
  19. 19. Working with HTML Forms <ul><li>You can easily get the variables submitted by an HTML form using the following (assume the form input is called “name”: </li></ul><ul><ul><li>$_POST['name'] // post method </li></ul></ul><ul><ul><li>$_GET['name'] // get method </li></ul></ul><ul><ul><li>$name /* easier, but Register Globals must be set to ON in PHP config */ </li></ul></ul>
  20. 20. Working with HTML Forms <ul><li>It is common to put the form and the results of different requests in the same file. </li></ul><ul><ul><li><HTML> </li></ul></ul><ul><ul><li><HEAD><TITLE>PHP FORM TEST</TITLE></HEAD> </li></ul></ul><ul><ul><li><BODY> </li></ul></ul><ul><ul><li><? </li></ul></ul><ul><ul><li>if (!isset($name) || $name == &quot;&quot;) { </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li><FORM METHOD=&quot;post&quot;> </li></ul></ul><ul><ul><li>Your name: <INPUT TYPE=&quot;text&quot; NAME=&quot;name&quot;> </li></ul></ul><ul><ul><li>Your age: <INPUT TYPE=&quot;text&quot; NAME=&quot;age&quot;> </li></ul></ul><ul><ul><li><INPUT TYPE=&quot;submit&quot;> </li></ul></ul><ul><ul><li></FORM> </li></ul></ul><ul><ul><li><? </li></ul></ul><ul><ul><li>} else { </li></ul></ul><ul><ul><li>echo &quot;Your name is $name<BR>&quot;; </li></ul></ul><ul><ul><li>echo &quot;Your age is $age&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li></BODY> </li></ul></ul><ul><ul><li></HTML> </li></ul></ul>If name is empty or not defined, then show the form If name is not empty, i.e., when the user has entered something, then show the results http://localhost/php/example2.php
  21. 21. PHP and MySQL <ul><li>PHP is designed to work with the MySQL database. However, it can also connect to other database systems such as Oracle, Sybase, etc., using ODBC. </li></ul>
  22. 22. Example <ul><ul><li><HTML> <BODY> </li></ul></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>$db = mysql_connect(&quot;localhost&quot;, &quot;root“,””); </li></ul></ul><ul><ul><li>mysql_select_db(&quot;mydb &quot; , $db); </li></ul></ul><ul><ul><li>$result = mysql_query(&quot;SELECT * FROM employees&quot;,$db); </li></ul></ul><ul><ul><li>printf(&quot;First Name: %s<br> &quot;, mysql_result($result,0,&quot;first&quot;)); </li></ul></ul><ul><ul><li>printf(&quot;Last Name: %s<br> &quot;, mysql_result($result,0,&quot;last&quot;)); </li></ul></ul><ul><ul><li>printf(&quot;Address: %s<br> &quot;, mysql_result($result,0,&quot;address&quot;)); </li></ul></ul><ul><ul><li>printf(&quot;Position: %s<br> &quot;,mysql_result($result,0,&quot;position&quot;)); </li></ul></ul><ul><ul><li>mysql_free_result($result); </li></ul></ul><ul><ul><li>mysql_close($db); </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li></BODY> </HTML> </li></ul></ul>
  23. 23. Useful PHP Functions for MySQL <ul><li>mysql_connect(host, username [,password]); </li></ul><ul><ul><li>Connects to a MySQL server on the specified host using the given username and/or password. Returns a MySQL link identifier on success, or FALSE on failure. </li></ul></ul><ul><li>mysql_select_db(db_name [,resource]) ‏ </li></ul><ul><ul><li>Selects a database from the database server. </li></ul></ul>
  24. 24. Useful PHP Functions for MySQL <ul><li>mysql_query(SQL, resource); </li></ul><ul><ul><li>Sends the specified SQL query to the database specified by the resource identifier. The retrieved data are returned by the function as a MySQL result set. </li></ul></ul><ul><li>mysql_result(result, row [,field]); </li></ul><ul><ul><li>Returns the contents of one cell from a MySQL result set. The field argument can be the field name or the field’s offset. </li></ul></ul><ul><li>mysql_fetch_array(result [,result_type]) ‏ </li></ul><ul><ul><li>Fetch a result row as an associative array, a numeric array, or both. The result type can take the constants MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. </li></ul></ul>
  25. 25. Useful PHP Functions for MySQL <ul><li>mysql_free_result(result) ‏ </li></ul><ul><ul><li>Frees the result set </li></ul></ul><ul><li>mysql_close(resource) ‏ </li></ul><ul><ul><li>Closes the connection to the database. </li></ul></ul>
  26. 26. Error Handling <ul><li>If there is error in the database connection, you can terminate the current script by using the die function. </li></ul><ul><li>For example: </li></ul><ul><li>$db = mysql_connect(&quot;localhost&quot;, &quot;root“, “”) </li></ul><ul><li>or die(&quot;Could not connect : &quot; . mysql_error()); </li></ul><ul><ul><li>mysql_select_db(&quot;my_database&quot;) </li></ul></ul><ul><ul><li>or die(&quot;Could not select database&quot;); </li></ul></ul><ul><li>$result = mysql_query($query) </li></ul><ul><li>or die(&quot;Query failed&quot;); </li></ul>
  27. 27. Example: Looping through the Cells <ul><ul><li><?php </li></ul></ul><ul><ul><li>/* Connecting, selecting database */ </li></ul></ul><ul><ul><li>$link = mysql_connect(&quot;mysql_host&quot;, &quot;mysql_user&quot;, mysql_password&quot;) ‏ </li></ul></ul><ul><ul><li>or die(&quot;Could not connect : &quot; . mysql_error()); </li></ul></ul><ul><ul><li>echo &quot;Connected successfully&quot;; </li></ul></ul><ul><ul><li>mysql_select_db(&quot;my_database&quot;) or die(&quot;Could not select database&quot;); </li></ul></ul><ul><ul><li>/* Performing SQL query */ </li></ul></ul><ul><ul><li>$query = &quot;SELECT * FROM my_table&quot;; </li></ul></ul><ul><ul><li>$result = mysql_query($query) </li></ul></ul><ul><ul><li>or die(&quot;Query failed : &quot; . mysql_error()); </li></ul></ul><ul><ul><li>/* Printing results in HTML */ </li></ul></ul><ul><ul><li>echo &quot;<table> &quot;; </li></ul></ul><ul><ul><li>while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { </li></ul></ul><ul><ul><li>echo &quot; <tr> &quot;; </li></ul></ul><ul><ul><li>foreach ($line as $col_value) { </li></ul></ul><ul><ul><li>echo &quot; <td>$col_value</td> &quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>echo &quot; </tr> &quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>echo &quot;</table> &quot;; </li></ul></ul>Loop through each row of the result set Loop through each element in a row
  28. 28. Example: Looping through the Cells <ul><ul><li>/* Free resultset */ </li></ul></ul><ul><ul><li>mysql_free_result($result); </li></ul></ul><ul><ul><li>/* Closing connection */ </li></ul></ul><ul><ul><li>mysql_close($link); </li></ul></ul><ul><ul><li>?> </li></ul></ul>
  29. 29. <ul><li>Thank You </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.