Php

35,757
-1

Published on

Learn PHP basics

Published in: Technology
1 Comment
6 Likes
Statistics
Notes
No Downloads
Views
Total Views
35,757
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
243
Comments
1
Likes
6
Embeds 0
No embeds

No notes for slide

Php

  1. 1. PHP Server Side Scripting Language www.php.net
  2. 2. What is the other side? <ul><li>JavaScript is the client side scripting language </li></ul><ul><li>JavaScript is integrated within your browser </li></ul><ul><li>It helps you do things on the user’s computer for e.g. form validation, simple calculation etc. </li></ul>
  3. 3. Why is PHP so powerful? <ul><li>Open Source and cross-platform </li></ul><ul><li>especially lightweight and speedy </li></ul><ul><li>can send HTTP headers </li></ul><ul><ul><li>set cookies </li></ul></ul><ul><ul><li>manage authentication </li></ul></ul><ul><ul><li>and redirect users </li></ul></ul><ul><li>excellent connectivity to many databases </li></ul><ul><li>integration with various external libraries </li></ul><ul><ul><li>to let you generate PDF documents </li></ul></ul><ul><ul><li>parse XML </li></ul></ul><ul><ul><li>create and modify images on the fly </li></ul></ul><ul><li>no need for a special development environment or IDE </li></ul><ul><ul><li>You start a block of PHP code with <?php and end it with ?> </li></ul></ul><ul><ul><li>PHP goes right into your Web pages </li></ul></ul><ul><li>You don't have to declare variables before you use them </li></ul><ul><li>it's easy to create arrays and hashes (associative arrays) </li></ul><ul><li>A good rule of thumb for PHP: &quot;When in doubt, try it; it will probably work.&quot; </li></ul>
  4. 4. PHP history <ul><li>In 1995, when Rasmus Lerdorf started what was to become PHP, the only goal he had in mind was to find out who was reading his resume. </li></ul><ul><li>Rasmus Lerdorf's original concept of creating tags within the HTML text for calling C code is revolutionizing the way dynamic Web applications are created. </li></ul><ul><li>Though the code written by him has been revised and the scripting engine re-written from scratch. Still, the concepts and the inspiration for PHP came from Lerdorf's approach of finding practical and dynamic solutions that Web developers could use in their day-to-day work. </li></ul>
  5. 5. PHP Server Side Scripting language Dynamic Web Pages Mail Cookies DataBase Support
  6. 6. PHP Basics <ul><li>PHP means PHP: Hypertext Preprocessor </li></ul><ul><li>Escaping from HTML <?php . . . ?> </li></ul><ul><li>Statements You use the semi-colon (;) at the end of the line </li></ul><ul><li>Comments PHP supports, C, C++ style comments. /* C, C++ style multi-line comment */ </li></ul>
  7. 7. Basic Syntax <ul><li><?php echo &quot;Hello, World!&quot;; ?> produces Hello, World! </li></ul><ul><li>Variables are marked with a preceding $. You could write &quot;Hello, World!&quot; like this: <?php $message = &quot;Hello, World!&quot;; echo $message; ?> </li></ul><ul><li>String concatenation is done with . (a period) </li></ul><ul><li>other arithmetic operators are what you would expect: <?php $greeting = &quot;Hello &quot;; $num = 3 + 2; $num++; echo &quot;$greeting $num people!&quot;; ?> produces Hello 6 people! </li></ul>
  8. 8. Single and double quote <ul><li>Just as in Perl, a string surrounded with double quotes causes variables inside it to be interpolated, but a string surrounded with single quotes does not. So, </li></ul><ul><li><?php $name = “Shantanu”; $greeting_1 = &quot;Hello, $name!&quot;; $greeting_2 = 'Hello, $name!'; echo &quot;$greeting_1 &quot;; echo &quot;$greeting_2 &quot;; ?> </li></ul><ul><ul><li>produces </li></ul></ul><ul><ul><li>Hello, Shantanu! Hello, $name! </li></ul></ul><ul><li>Note that the in the string turns into a new line, just as in Perl or in C. This only works in double-quoted strings, however. </li></ul>
  9. 9. Arrays <ul><li>You set off array indices (regular or associative) with square brackets ([ and ]): $fruit[0] = 'banana'; $fruit[1] = 'papaya'; </li></ul><ul><li>A shortcut for creating arrays is the array() $fruit = array('banana','papaya'); </li></ul><ul><li>The built-in function count() tells you how many elements are in an array: $fruit = array('banana','papaya'); print count($fruit); prints 2 </li></ul>
  10. 10. Arrays <ul><li>Array index starts from 0 </li></ul><ul><li>$a[0] = &quot;first&quot;; // a two element array $a[1] = &quot;second&quot;; $a[] = &quot;third&quot;; // easy way to append to the array. // Now, $a[2] has &quot;third&quot; </li></ul><ul><li>echo count($a); // prints 3 </li></ul><ul><li>// create a hash in one shot </li></ul><ul><li>$myphonebook = array ( </li></ul><ul><li>&quot;sbabu&quot; => &quot;5348&quot;, </li></ul><ul><li>&quot;keith&quot; => &quot;4829&quot;, </li></ul><ul><li>&quot;carole&quot; => &quot;4533&quot; </li></ul><ul><li>); </li></ul><ul><li>// oops, forgot dean! add dean </li></ul><ul><li>$myphonebook[&quot;dean&quot;] = &quot;5397&quot;; </li></ul><ul><li>// yeoh! carole's number is not that. correct it! </li></ul><ul><li>$myphonebook[&quot;carole&quot; => &quot;4522&quot; </li></ul><ul><li>// didn't we tell that hashes and arrays are </li></ul><ul><li>//implemented alike? let's see </li></ul><ul><li>echo &quot;$myphonebook[0]&quot;; // sbabu </li></ul><ul><li>echo &quot;$myphonebook[1]&quot;; // 5348 </li></ul><ul><li>Some functions useful in this context are sort(), next(), prev(), each(). </li></ul>
  11. 11. Environment Variables <ul><li>If the page &quot;http://domain.com/company/test.php” contains the code <?php echo &quot;[$REQUEST_URI]&quot;; ?> it prints out [/company/test.php] </li></ul>
  12. 12. Custom Functions <ul><li>You can define your own functions as shown below. Return values can be any of the data types. </li></ul><ul><li>function foo ($arg_1, $arg_2, ..., $arg_n) { </li></ul><ul><li>echo &quot;Example function. &quot;; </li></ul><ul><li>return $retval; </li></ul><ul><li>} </li></ul><ul><li>Any valid PHP code may appear inside a function, even other functions and class definitions. Functions must be defined before they are referenced. </li></ul>
  13. 13. Custom Functions - Example <ul><li><? </li></ul><ul><li>function AddFive ($TheNumber) </li></ul><ul><li>{ </li></ul><ul><li>$TheNumberPlusFive = $TheNumber + 5; </li></ul><ul><li>print(&quot;$TheNumber plus 5 equals $TheNumberPlusFive.<br>&quot;); </li></ul><ul><li>} </li></ul><ul><li>AddFive(15); </li></ul><ul><li>AddFive(23); </li></ul><ul><li>?> </li></ul>
  14. 14. Objects <ul><li>To create an object, use the new statement. </li></ul><ul><li>class foo { </li></ul><ul><li>function do_foo () { </li></ul><ul><li>echo &quot;Doing foo.&quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>$bar = new foo; </li></ul><ul><li>$bar->do_foo(); </li></ul>
  15. 15. Classes <ul><li>class Employee { </li></ul><ul><li>var $empno; // employee number </li></ul><ul><li>var $empnm; // name </li></ul><ul><li>function add_employee($in_num, $in_name){ </li></ul><ul><li>$this->empno = $in_num; </li></ul><ul><li>$this->empnm = $in_name; </li></ul><ul><li>} </li></ul><ul><li>function show() { </li></ul><ul><li>echo &quot;$this->empno, $this->empnm&quot;; </li></ul><ul><li>return; </li></ul><ul><li>} </li></ul><ul><li>function changenm($in_name){ </li></ul><ul><li>$this->empnm = $in_name; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>$sbabu = new Employee; </li></ul><ul><li>$sbabu->add_employee(10,&quot;sbabu&quot;); </li></ul><ul><li>$sbabu->changenm(&quot;babu&quot;); </li></ul><ul><li>$sbabu->show(); </li></ul>
  16. 16. Guest Book Example <ul><li>guest.htm </li></ul><ul><ul><li>A form where the site visitor will type his name and message. </li></ul></ul><ul><ul><li>Submit button will call the guestbook.php file. </li></ul></ul><ul><li>guestbook.php </li></ul><ul><ul><li>open the guestbook.txt file </li></ul></ul><ul><ul><li>read old contents, add the new comment </li></ul></ul><ul><ul><li>display all comments to the browser </li></ul></ul><ul><ul><li>also add the new comment to the guestbook.txt file </li></ul></ul><ul><li>guestbook.txt </li></ul><ul><ul><li>Starts with welcome message. </li></ul></ul><ul><ul><li>Keeps adding the comments added by users. </li></ul></ul><ul><ul><li>Don't forget to change the file permission from read to write once uploaded to the server. </li></ul></ul>
  17. 17. guest.htm <ul><li><body> </li></ul><ul><li><form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;guestbook.php&quot;> </li></ul><ul><li><input type=&quot;text&quot; name=&quot;visitor&quot;><p> </li></ul><ul><li><textarea name=&quot;comment&quot; rows=&quot;10&quot; cols=&quot;40&quot;></textarea><p> </li></ul><ul><li><input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;> </li></ul><ul><li></form> </li></ul><ul><li></body> </li></ul>
  18. 18. guestbook.php <ul><li><? </li></ul><ul><li>$file=&quot;guestbook.txt&quot;; </li></ul><ul><li>$handle=fopen($file, &quot;r+&quot;); </li></ul><ul><li>$prevtext=fread($handle,filesize(&quot;$file&quot;)); </li></ul><ul><li>$new=&quot;<div align=&quot;center&quot;> Posted by: <u><b>$visitor</b></u> <br> <p><font face=&quot;Fixedsys&quot;> $comment </font></p> <hr> </div>&quot;; </li></ul><ul><li>$total = $prevtext . $new; </li></ul><ul><li>fclose($handle); </li></ul><ul><li>echo &quot;$total&quot;; </li></ul><ul><li>$handle=fopen($file, &quot;w&quot;); </li></ul><ul><li>fwrite($handle, $total); </li></ul><ul><li>fclose($handle); </li></ul><ul><li>?> </li></ul>
  19. 19. guestbook.txt <ul><li><p>Welcome</p> </li></ul>
  20. 20. Cookies <ul><li>To create and modify a cookie, use the PHP function setcookie(). setcookie() takes up to six arguments, depending upon how much control you want over the cookie and who can read its value. </li></ul><ul><li>The simplest way of setting a cookie is like this: setcookie('name', 'bret'); </li></ul>
  21. 21. Add the following to the top of guestbook.php <ul><li><?php </li></ul><ul><li>if (!$mevisit1){ </li></ul><ul><li>@setcookie(&quot;mevisit1&quot;, &quot;$visitor&quot;, time()+3600); </li></ul><ul><li>} </li></ul><ul><li>If ($mevisit1 != $visitor){ </li></ul><ul><li>echo &quot;hi $mevisit1!, we know you are not $visitor!!&quot;; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>echo &quot;welcome back, $visitor&quot;; </li></ul><ul><li>… </li></ul><ul><li>} </li></ul>
  22. 22. Variables through URL <ul><li><html> </li></ul><ul><li><font color=<?php echo $mycolor; ?>>THis color is decided in the variable</font> </li></ul><ul><li></html> </li></ul><ul><li>test.php?mycolor=blue </li></ul>
  23. 23. Control Structure (If - Else) <ul><li>You can use the control structures if and elseif: if ($user_count > 200) { print &quot;The site is busy right now!&quot;; } elseif ($user_count > 100) { print &quot;The site is sort of active right now!&quot;; } else { print &quot;The site is lonely - only $user_count user logged on.&quot;; } </li></ul>
  24. 24. Control Structure (while or for) <ul><li>To Print the following you can use looping structures such as for and while : I have eaten 4 bagels today. I have eaten 5 bagels today. I have eaten 6 bagels today. I have eaten 7 bagels today. </li></ul><ul><li>$i = 4; while ($i < 8) { print &quot;I have eaten $i bagels today. &quot;; $i++; } </li></ul><ul><li>for ($i = 4; $i < 8; $i++) { print &quot;I have eaten $i bagels today. &quot;; } </li></ul>
  25. 25. Control Structure (foreach) <ul><li><?php </li></ul><ul><li>$month = array(&quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, &quot;Novemeber&quot;, &quot;December&quot;); </li></ul><ul><li>echo &quot;<FORM METHOD=POST ACTION = results.php>&quot;; </li></ul><ul><li>echo &quot;<SELECT NAME=column_name >&quot;; </li></ul><ul><li>echo&quot;<OPTION VALUE=&quot;/&quot;>Pick a Month</OPTION>&quot;; </li></ul><ul><li>foreach($month as $MON) </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;<OPTION VALUE=&quot;$MON&quot;>$MON</OPTION>&quot;; </li></ul><ul><li>} </li></ul><ul><li>echo &quot;</SELECT></FORM>&quot;; </li></ul><ul><li>?> </li></ul>
  26. 26. Switch <ul><li>Switch is an elegant replacement for multiple if-elseif-else structure. </li></ul><ul><li>switch ($i) { </li></ul><ul><li>case 0: </li></ul><ul><li>print &quot;i equals 0&quot;; </li></ul><ul><li>case 1: </li></ul><ul><li>print &quot;i equals 1&quot;; </li></ul><ul><li>case 2: </li></ul><ul><li>print &quot;i equals 2&quot;; </li></ul><ul><li>} </li></ul><ul><li>break, continue </li></ul><ul><li>break breaks out of current looping control-scructures. </li></ul><ul><li>continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the beginning of the next iteration. </li></ul>
  27. 27. Mobile Handset Dealer’s website Example <ul><li>index.php The file with a drop down option. </li></ul><ul><li>myfiles/photos.htm A html file with photos section. </li></ul><ul><li>myfiles/products.htm A html file with products section. </li></ul><ul><li>myfiles/printer.php This .php file will display the contents of the current page with the help of printer.css stylesheet. </li></ul><ul><li>myfiles/normal.css Normal stylesheet file. </li></ul><ul><li>myfiles/printer.css Printer friendly stylesheet file. </li></ul><ul><li>myfiles/A6188.gif </li></ul>
  28. 28. Index.php <ul><li><HTML> </li></ul><ul><li><HEAD> </li></ul><ul><li><LINK rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;include/normal.css&quot;> </li></ul><ul><li></HEAD> </li></ul><ul><li><BODY> </li></ul><ul><li><FORM name=&quot;infosys&quot;> </li></ul><ul><li><SELECT size=&quot;1&quot; </li></ul><ul><li>onchange=&quot;window.location=document.infosys.navigation.options[document.infosys.navigation.selectedIndex].value&quot; name=&quot;navigation&quot;> </li></ul><ul><li><OPTION selected>-------------------</OPTION> </li></ul><ul><li><OPTION value=&quot;index.php?do=photos&quot;>photos </OPTION> </li></ul><ul><li><OPTION value=&quot;index.php?do=products&quot;>products</OPTION> </li></ul><ul><li></SELECT> </li></ul><ul><li><?php </li></ul><ul><li>switch ($do) { </li></ul><ul><li>case &quot;photos&quot;: </li></ul><ul><li> include “myfiles/photos.htm&quot;; </li></ul><ul><li>$printer=“myfiles/printer.php?print_mode=1&&doc=photos.htm&quot;; </li></ul><ul><li>break; </li></ul><ul><li>case &quot;products&quot;: </li></ul><ul><li> include “myfiles/products.php&quot;; </li></ul><ul><li>$printer=“myfiles/printer.php?print_mode=1&&doc=products.htm&quot;; </li></ul><ul><li> break; </li></ul><ul><li>} ?> </li></ul><ul><li></FORM> </li></ul><ul><li><P>Want to print? <a href=&quot;<?php echo &quot;$printer&quot; ?>&quot;>Printer Friendly Page</a> </P> </li></ul><ul><li></BODY> </li></ul><ul><li></HTML> </li></ul>
  29. 29. myfiles/photos.htm <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>Photo Gallery</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><P> Please visit our photo gallery </li></ul><ul><li><a href=“http://yahoo.com/photos&quot;>Here</a> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  30. 30. Myfiles/products.htm <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>Product specification</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><p>Motorola A6188 </P> Welcome to your new office. The new Motorola Accompli A6188 is a Chinese/English WAP-enabled PDA and mobile phone. Which makes it the world's smartest way to..<P> </li></ul><ul><li><img src=include/a6188.gif> </li></ul><ul><li><TABLE> </li></ul><ul><li><TR> <TD>Device Name </TD> <TD>Accompli A6188 </TD> </TR> </li></ul><ul><li><TR> <TD>GSM 900 </TD> <TD> Yes</TD> </TR> </li></ul><ul><li><TR> <TD>Input Method </TD> <TD>On-screen keyboard/Voice Recognition </TD> </TR> </li></ul><ul><li></TABLE> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  31. 31. myfiles/printer.php <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><STYLE TYPE=&quot;text/css&quot;> </li></ul><ul><li><?PHP </li></ul><ul><li>if ($print_mode != 1) { </li></ul><ul><li>include 'normal.css'; </li></ul><ul><li>} else { </li></ul><ul><li>include 'printer.css'; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li></STYLE> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li>Print Friendly Page: </li></ul><ul><li><?PHP </li></ul><ul><li>include &quot;$doc&quot;; </li></ul><ul><li>?> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  32. 32. myfiles/normal.css myfiles/printer.css <ul><li>normal.css </li></ul><ul><li>body { color: white; background-color: #003366; font-size: 12pt} </li></ul><ul><li>A{color:#6666FF; text-decoration:none} </li></ul><ul><li>A:visited{color:#6666FF; text-decoration:none} </li></ul><ul><li>A:active{color:#6666FF; text-decoration:none} </li></ul><ul><li>A:hover{color:#6666FF; text-decoration:underline} </li></ul><ul><li>printer.css </li></ul><ul><li>BODY {margin: 2em; color: black; font-size: 12pt; text-indent: 2em; </li></ul><ul><li>/* i.e. 36pt */} </li></ul><ul><li>IMG {display: none} </li></ul><ul><li>table {font-size: 12pt; background: none; width: 100%;} </li></ul>
  33. 33. myfiles/a6188.gif
  34. 34. Estate broker example <ul><li>broker.htm A simple form with 4 fields Name, Surname, FlatHave and FlatWant with a submit button that will call the process_form.php file. </li></ul><ul><li>process_form.php The variables $Name, $Surname, $FlatHave and $FlatWant are added to the “list” table of “broker” database. A mail is sent to the manager about the new entry added. </li></ul><ul><li>display.php Display everything from “list” table. </li></ul>
  35. 35. broker.htm <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title> Add Record</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li>Please add the record here... </li></ul><ul><li><FORM name=&quot;myform&quot; ACTION=&quot;process_form.php&quot; METHOD=&quot;POST&quot;> </li></ul><ul><li>Your Name: <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;Name&quot;><br> </li></ul><ul><li>Your Surname: <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;Surname&quot;><br> </li></ul><ul><li>Flat Have at: <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;FlatHave&quot;><br> </li></ul><ul><li>Flat Want at: <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;FlatWant&quot;><br> </li></ul><ul><li><INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;Add Record&quot; NAME=&quot;sendit&quot;> </li></ul><ul><li><INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;Cancel&quot; NAME=&quot;cancelit&quot;><br> </li></ul><ul><li></FORM> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  36. 36. process_form.php <ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><?php </li></ul><ul><li>mysql_connect(&quot;&quot;,&quot;&quot;,&quot;&quot;); </li></ul><ul><li>mysql_select_db(&quot;broker&quot;); </li></ul><ul><li>mysql_query (&quot;INSERT INTO list values (´´, ´$Name´, ´$Surname´, ´$FlatHave´, ´$FlatWant´);&quot;); </li></ul><ul><li>?> </li></ul><ul><li>Thank you for your entry. </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  37. 37. Mail <ul><li>Now can you send an e-mail to the manager whenever someone adds an entry? </li></ul><ul><li>Add the following code in the process_form.php file </li></ul><ul><li><?php </li></ul><ul><li>mail(&quot;shantanuo@hotmail.com&quot;, &quot;Entry Added&quot;,&quot; </li></ul><ul><li>Name : $Name </li></ul><ul><li>Surname : $Surname </li></ul><ul><li>Flat Have : $FlatHave </li></ul><ul><li>Flat Want : $FlatWant </li></ul><ul><li>&quot;); </li></ul><ul><li>?> </li></ul>
  38. 38. display.php <ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li>The data stored into the “broker” Database -“List” Table. </li></ul><ul><li><?php </li></ul><ul><li>mysql_connect(&quot;&quot;,&quot;&quot;,&quot;&quot;); </li></ul><ul><li>mysql_select_db(&quot;broker&quot;); </li></ul><ul><li>$query = &quot;select * from list&quot;; </li></ul><ul><li>$result = mysql_query ($query); </li></ul><ul><li>while($row = mysql_fetch_array($result)) </li></ul><ul><li>{ </li></ul><ul><li>echo $row[Name] .&quot;<br>&quot;. $row[Surname].&quot;</p>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  39. 39. contents of database student Part I <ul><li><?php </li></ul><ul><li>$dbname = &quot;student&quot;; </li></ul><ul><li>$loginname = &quot;shaileshr21&quot;; </li></ul><ul><li>$loginpass = &quot;&quot;; </li></ul><ul><li>$dbhost = &quot;mysql&quot;; </li></ul><ul><li>echo('<html><body bgcolor=&quot;#FFFFFF&quot;>'); </li></ul><ul><li>echo('<font face=&quot;arial&quot; size=&quot;+4&quot;><center>'); </li></ul><ul><li>echo(&quot; $dbname&quot;); </li></ul><ul><li>$id_link = @mysql_connect($dbhost, $loginname, $loginpass); </li></ul><ul><li>$tables = mysql_list_tables($dbname, $id_link); </li></ul><ul><li>$num_tables = mysql_num_rows($tables); </li></ul><ul><li>// store table names in an array </li></ul><ul><li>$arr_tablenames[] = ''; </li></ul>
  40. 40. contents of database student Part II <ul><li>// store number of fields per table(index 0,1,2..) in an array </li></ul><ul><li>$arr_num_fields[] = ''; </li></ul><ul><li>for ($i=0; $i < $num_tables; $i++) { </li></ul><ul><li>$arr_tablenames[$i] = mysql_tablename($tables, $i); </li></ul><ul><li>$arr_num_fields[$i] = mysql_num_fields(mysql_db_query($dbname, &quot;select * from $arr_tablenames[$i]&quot;, $id_link)); </li></ul><ul><li>} </li></ul><ul><li>// store field names in a multidimensional array: </li></ul><ul><li>// [i] == table number, [ii] == field number for that table </li></ul><ul><li>for ($i=0; $i < $num_tables; $i++) { </li></ul><ul><li>for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) { </li></ul><ul><li>$result = mysql_db_query($dbname, &quot;select * from $arr_tablenames[$i]&quot;, $id_link); </li></ul><ul><li>$hash_field_names[$i][$ii] = mysql_field_name($result, $ii); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  41. 41. contents of database student Part III <ul><li>for ($i=0; $i < $num_tables; $i++) { </li></ul><ul><li>echo(&quot;<center><h2> $arr_tablenames[$i] </h2></center>&quot;); </li></ul><ul><li>echo('<table align=&quot;center&quot; border=&quot;1&quot;><tr>'); </li></ul><ul><li>$result = mysql_db_query($dbname, &quot;select * from $arr_tablenames[$i]&quot;, $id_link); </li></ul><ul><li>for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) { </li></ul><ul><li>echo(&quot;<th>&quot;); </li></ul><ul><li>echo $hash_field_names[$i][$ii]; </li></ul><ul><li>echo(&quot;</th>&quot;); </li></ul><ul><li>} </li></ul><ul><li>echo(&quot;</tr><tr>&quot;); </li></ul><ul><li>$number_of_rows = @mysql_num_rows($result); </li></ul><ul><li>for ($iii = 0; $iii < $number_of_rows; $iii++) { </li></ul><ul><li>$record = @mysql_fetch_row($result); </li></ul><ul><li>for ($ii=0; $ii < $arr_num_fields[$i]; $ii++) { </li></ul><ul><li>echo(&quot;<td>&quot;); </li></ul><ul><li>// echo $record[$ii]; </li></ul><ul><li>$mytext = nl2br($record[$ii]); </li></ul><ul><li>echo $mytext; </li></ul><ul><li>echo(&quot;</td>&quot;); </li></ul><ul><li>} </li></ul><ul><li>echo(&quot;</tr>&quot;); </li></ul><ul><li>} </li></ul><ul><li>echo(&quot;</table>&quot;); </li></ul><ul><li>} </li></ul><ul><li>echo('</body></html>'); </li></ul><ul><li>?> </li></ul>
  42. 42. Display Bookmarks Part I <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><basefont face=&quot;Arial&quot;> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><?php </li></ul><ul><li>// set database server access variables: </li></ul><ul><li>$host = &quot;mysql&quot;; </li></ul><ul><li>$user = &quot;shaileshr21&quot;; </li></ul><ul><li>$pass = &quot;&quot;; </li></ul><ul><li>$db = &quot;student&quot;; </li></ul><ul><li>// open connection </li></ul><ul><li>$connection = mysql_connect($host, $user, $pass) or die (&quot;Unable to connect!&quot;); </li></ul><ul><li>// select database </li></ul><ul><li>mysql_select_db($db) or die (&quot;Unable to select database!&quot;); </li></ul><ul><li>// create query </li></ul><ul><li>$query = &quot;select concat('<a href=&quot;',Website,'&quot;>', Website, '</a>'), Explanation from Bookmarks where Catgegory='$mycat'&quot;; </li></ul>
  43. 43. Display Bookmarks Part II <ul><li>echo &quot;<h2>$mycat</h2>&quot;; </li></ul><ul><li>// execute query </li></ul><ul><li>$result = mysql_query($query) or die (&quot;Error in query: $query. &quot;.mysql_error()); </li></ul><ul><li>// see if any rows were returned </li></ul><ul><li>if (mysql_num_rows($result) > 0) { </li></ul><ul><li>// yes </li></ul><ul><li>// print them one after another </li></ul><ul><li>echo &quot;<table cellpadding=10 border=1>&quot;; </li></ul><ul><li>while($row = mysql_fetch_row($result)) { </li></ul><ul><li>echo &quot;<tr>&quot;; </li></ul><ul><li>echo &quot;<td>&quot;.$row[0].&quot;</td>&quot;; </li></ul><ul><li>echo &quot;<td>&quot; . $row[1].&quot;</td>&quot;; </li></ul><ul><li>echo &quot;</tr>&quot;; </li></ul><ul><li>} </li></ul><ul><li>echo &quot;</table>&quot;; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>// print status message </li></ul><ul><li>echo &quot;No rows found!&quot;; </li></ul><ul><li>} </li></ul><ul><li>// free result set memory </li></ul><ul><li>mysql_free_result($result); </li></ul><ul><li>mysql_close($connection); </li></ul><ul><li>?> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  44. 44. Mysql front-ends <ul><li>phpMyAdmin Search across tables, Most Popular, (free software) </li></ul><ul><li>phpFlashMyAdmin User friendly flash interface, graphical representation of innodb tables, (Paid) </li></ul><ul><li>MySql Data Manager Better Administrative interface in perl (Paid) </li></ul>
  45. 45. Mysql interface <ul><li>CSV to MySQL http://www.sqldbu.com/eng/sections/tips/normalize.html Upload the .csv and get the create table and insert statements </li></ul><ul><li>MySQL to CSV http://saraswaticlasses.com/sites/dump.php Type the table name and password to download the records in the excel form. </li></ul><ul><li>MySQL to RSS http://saraswaticlasses.com/sites/createrss.htm Publish the records from your mysql database using the RSS feed without any web interface. </li></ul>
  46. 46. Simple RSS Reader <ul><li><?php </li></ul><ul><li>$cookies = array(); </li></ul><ul><li>getcookies(); </li></ul><ul><li>if ($_GET[&quot;deleteall&quot;]==&quot;yes&quot;) { </li></ul><ul><li>foreach($cookies as $id=>$url) { </li></ul><ul><li>deletecookie($id); </li></ul><ul><li>} </li></ul><ul><li>header(&quot;Location: $PHP_SELF&quot;); </li></ul><ul><li>} elseif(is_numeric($_GET[&quot;delete&quot;])) { </li></ul><ul><li>deletecookie($id); </li></ul><ul><li>header(&quot;Location: $PHP_SELF&quot;); </li></ul><ul><li>} elseif(!empty($_GET[&quot;formfeedurl&quot;])) { </li></ul><ul><li>addcookie($formfeedurl); </li></ul><ul><li>header(&quot;Location: $PHP_SELF&quot;); </li></ul><ul><li>} elseif ($cookiecount > 0) { </li></ul><ul><li>foreach($cookies as $id=>$url) { </li></ul><ul><li>echo &quot;<!-- FeedCookie[$id] == $url -->&quot;; // For debugging, or something. </li></ul><ul><li>?> </li></ul><ul><li><script language=&quot;javascript&quot; src=&quot;http://p3k.org/rss/index.r?url=<?php echo $url; ?>%3Ff%3D7&amp;align=left&amp;width=300&amp;frameColor=black&amp;titleBarColor=%23add8e6&amp;titleBarTextColor=black&amp;boxFillColor=white&amp;textColor=black&amp;fontFace=&amp;maxItems=7&amp;compact=&amp;showXmlButton=&amp;javascript=true&quot;&quot;></script> </li></ul><ul><li><?php </li></ul><ul><li>} </li></ul><ul><li>} else { </li></ul><ul><li>// There are no feeds defined. </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  47. 47. Simple RSS Reader II <ul><li><p style=&quot;clear: both&quot;> </li></ul><ul><li><form action=&quot;<?php echo $PHP_SELF; ?>&quot;> </li></ul><ul><li><input type=&quot;text&quot; name=&quot;formfeedurl&quot; size=&quot;26&quot;> </li></ul><ul><li><input type=&quot;submit&quot; name=&quot;Add&quot; value=&quot;Add Feed&quot;> </li></ul><ul><li></form> </li></ul><ul><li><a href=&quot;<?php echo $PHP_SELF; ?>?deleteall=yes&quot;>Delete All Cookies</a><p> </li></ul><ul><li><a href=&quot;<?php echo $PHP_SELF; ?>?source=yes&quot;>Source Code</a><p> </li></ul>
  48. 48. Simple RSS Reader Part III <ul><li><?php </li></ul><ul><li>function getcookies() { </li></ul><ul><li>global $cookiecount, $cookies; </li></ul><ul><li>foreach ($_COOKIE as $name=>$value) { </li></ul><ul><li>if (ereg(&quot;^FeedCookie-([0-9]{1,2})$&quot;,$name,$regs)) { </li></ul><ul><li>$id = (int)$regs[1]; </li></ul><ul><li>$cookies[$id] = $value; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>$cookiecount = count($cookies); </li></ul><ul><li>} </li></ul><ul><li>function addcookie($value) { </li></ul><ul><li>global $cookies; </li></ul><ul><li>for ($i=0; $i<100; $i++) { </li></ul><ul><li>if (!isset($cookies[$i])) { </li></ul><ul><li>$id = $i; </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>if (!isset($id)) { </li></ul><ul><li>echo &quot;No ID.&quot;; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul><ul><li>setcookie(&quot;FeedCookie-$id&quot;,$value,time()+31536000); </li></ul><ul><li>return 1; </li></ul><ul><li>} </li></ul><ul><li>function deletecookie($id) { </li></ul><ul><li>global $cookies; </li></ul><ul><li>if (isset($cookies[$id])) { </li></ul><ul><li>setcookie(&quot;FeedCookie-$id&quot;,&quot;&quot;,time()-3600); </li></ul><ul><li>return 1; </li></ul><ul><li>} else { </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>if ($source==yes) { </li></ul><ul><li>$source = 'simplefeed.php'; </li></ul><ul><li>highlight_file( $source ); </li></ul><ul><li>} </li></ul><ul><li>?> </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.

×