P H P Part I I, By Kian

5,120 views

Published on

Published in: Technology
4 Comments
13 Likes
Statistics
Notes
  • its very helpful for me, please email a copy of this at naresh2629@gmail.com

    thanks
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • it very helpful to me
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • this ppt is concise and informative, can you also email a copy to me at
    billsfchan@yahoo.com
    thanks

    Bill
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • hello...this ppt is very useful for me,,,can u plz mail it to me at apratiks@gmail.com
    thanks
    pratiksha
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
5,120
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
0
Comments
4
Likes
13
Embeds 0
No embeds

No notes for slide

P H P Part I I, By Kian

  1. 1. Inscribed By: Kian  PHP & MySQL & Dreamweaver
  2. 2. PART II <ul><li>Installation & Hands On Practice! </li></ul><ul><li> </li></ul>
  3. 3. Installations <ul><li>Installing devphp package </li></ul><ul><li>Testing the Apache Server by applying PHP codes </li></ul><ul><li>Installing MySQL-Front </li></ul><ul><li>Installing an Editor (Crimson) </li></ul><ul><li>Installing a Designing Application Program (Dearmweaver) </li></ul>
  4. 4. Testing PHP <ul><li>Create the following hello.php script </li></ul><ul><li>Put it in C:phpdevwwwhello.php </li></ul><html> <head><title>Hello Script</title></head> <body> <?php echo &quot;<h1>Hello PHP World!</h1>&quot; ?> (or <?php echo phpinfo(); ?>) </body> </html>
  5. 5. Exercise: Creating A HTML Form <ul><li><html> </li></ul><ul><li><head>A HTML Page!</head> </li></ul><ul><li><body> </li></ul><ul><li><b>Survey Form</b> </li></ul><ul><li><form method = &quot;post&quot; action=“survey.php&quot;> </li></ul><ul><li>What is your name? <input name = &quot;Name&quot; type = &quot;text&quot;> </li></ul><ul><li><br> </li></ul><ul><li>What is your age? <input name = &quot;Age&quot; type = &quot;text&quot;> </li></ul><ul><li><br> </li></ul><ul><li>Which country are you from? </li></ul><ul><li><select name = &quot;Country&quot;> </li></ul><ul><li><option value = &quot;Malaysia&quot;>Malaysia</option> </li></ul><ul><li><option value = &quot;Thailand&quot;>Thailand</option> </li></ul><ul><li><option value = &quot;Singapore&quot;>Singapore</option> </li></ul><ul><li><option value = &quot;Others&quot;>Others</option> </li></ul><ul><li></select> </li></ul><ul><li><br><br> </li></ul><ul><li><input type=&quot;submit&quot; name=&quot;Submit&quot;> </li></ul><ul><li></form></body></html> </li></ul>save as form.html
  6. 9. MySQL <ul><li>Open source database. </li></ul><ul><li>Data & information </li></ul><ul><li>Data Types are: </li></ul><ul><ul><li>char </li></ul></ul><ul><ul><li>varchar </li></ul></ul><ul><ul><li>int </li></ul></ul><ul><ul><li>text </li></ul></ul><ul><ul><li>float </li></ul></ul><ul><ul><li>date </li></ul></ul><ul><ul><li>time </li></ul></ul>
  7. 10. <ul><li>mysql_connect(server, username, password) </li></ul><ul><ul><li>connects to a MySQL server through a port </li></ul></ul><ul><ul><li>the default is the string &quot;localhost:3306&quot; </li></ul></ul><ul><ul><li>username is a string for the user name </li></ul></ul><ul><ul><li>password is a string for the password </li></ul></ul><ul><ul><li>returns TRUE on success else FALSE </li></ul></ul><ul><li>$database = mysql_connect(&quot;localhost&quot;, “&quot;, &quot;&quot;) </li></ul><ul><li>Mysql_close(database_connection); </li></ul>Connecting To A Database
  8. 11. MySQL Window Using MySQL Front
  9. 12. SQL <ul><li>SQL-Std Language for Interacting With most Databases. SQL commands can be divided into 2 main sub languages: </li></ul><ul><li>D ata D escription L anguage ( DDL ): </li></ul><ul><ul><li>(Create, Alter, Drop) </li></ul></ul><ul><li>D ata M anipulation L anguage ( DML ): </li></ul><ul><ul><li>(Insert, Update ,Delete) </li></ul></ul><ul><li>Data Retrieval: </li></ul><ul><ul><li>(Select) </li></ul></ul><ul><li>Join Statements: </li></ul><ul><ul><li>(Join) </li></ul></ul>
  10. 13. MySQL & PHP <ul><li>Sample PHP-MySQL application </li></ul><ul><li>Connect to MySQL database server </li></ul><ul><li>Send SQL query to MySQL server and get the results </li></ul><ul><li>Use the A pplication P rogramming I nterface’s ( API ) to get data from the result that is returned in step2 </li></ul><ul><li>Generate an HTML page to display the contents </li></ul>
  11. 14. MySQL API Support in PHP (PHP Functions for MySQL) <ul><li>mysql_connect (server-name, user-name, password); </li></ul><ul><li>Attempts to open a connection to a MySQL server. Returns a link-identifier on success or false on failure. </li></ul><ul><li>mysql_create_db (database-name, link-identifier); </li></ul><ul><li>Attempts to create a new database on the server associated with the specified link-identifier. Returns true on success or false on failure. </li></ul><ul><li>mysql_select_db (database-name, link-identifier); </li></ul><ul><li>Select the current active database on the server that’s associated with the specified link-identifier. Returns true on success or false on failure. </li></ul>
  12. 15. MySQL API Support in PHP (PHP Functions for MySQL) <ul><li>mysql_drop_db (database-name, link-identifier); </li></ul><ul><li>Attempts to delete an entire database on the server associated with the specified link-identifier . Returns true on success or false on failure. </li></ul><ul><li>mysql_fetch_array (result-resource); </li></ul><ul><li>Returns an array that corresponds to a database table row or returns false if there are no more rows. </li></ul><ul><li>mysql_num_rows (result-resource); </li></ul><ul><li>Returns the number of table rows in a result-resource . </li></ul>
  13. 16. MySQL API Support in PHP (PHP Functions for MySQL) <ul><li>mysql_list_dbs (link-identifier); </li></ul><ul><li>Lists the databases available on a MySQL server. </li></ul><ul><li>mysql_list_tables (database-name, link-identifier); </li></ul><ul><li>Lists tables in the specified database. </li></ul><ul><li>mysql_query (SQL-query-string, link-identifier); </li></ul><ul><li>Sends a query to the currently active database on the server that’s associated with the specified link-identifier. Returns a result-resource answering the query on success or false on failure. </li></ul>
  14. 17. MySQL API Support in PHP <ul><li>Mysql_connect </li></ul><ul><li>int mysql_connect(string hostname,string username,string password) </li></ul><ul><li>eg $conn=mysql_connect(&quot;localhost&quot;,&quot;root&quot;,“ &quot;); </li></ul>
  15. 18. <ul><li>Mysql_select_db </li></ul><ul><li>Int mysql_sellect_db(string database_name,int connection_identifier) </li></ul><ul><li>Eg mysql_select_db(&quot;studentdb&quot;,$conn);-returns true on success and false on failure </li></ul><ul><li>Mysql_query </li></ul><ul><li>Int mysql_query(string query) </li></ul><ul><li>Eg $result=mysql_query(&quot;SELECT * FROM students&quot;); </li></ul><ul><li>It contains the result of the execution of the query. </li></ul>
  16. 19. <ul><li>Mysql_num_rows </li></ul><ul><li>Int mysql_num_rows(int result) </li></ul><ul><li>It returns the number of rows in the result identifier.Eg $num_rows=mysql_num_rows($result); </li></ul><ul><li>Mysql_fetch_array </li></ul><ul><li>Fetches the row as an associative array </li></ul><ul><li>Array mysql_fetch_array(int result_identifier) </li></ul><ul><li>Eg $row=mysql_fetch_array($result) </li></ul>
  17. 20. <ul><li>Mysql_close </li></ul><ul><li>int mysql_close(int link_identifier) </li></ul><ul><li>mysql_close($conn) </li></ul>
  18. 21. Connection <ul><li><? </li></ul><ul><li>$connection=mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;&quot;); </li></ul><ul><li>if(!connection) </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;Unable to connect to the database server&quot;; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;Connected&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  19. 22. Connecting To Database <ul><li><? </li></ul><ul><li>mysql_select_db(&quot;studentdb&quot;,$connection); </li></ul><ul><li>echo &quot;Connected&quot;; </li></ul><ul><li>?> </li></ul>
  20. 23. Retrieving <ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><h1>Students Details</h1> </li></ul><ul><li><table><tr><td>StudentId</td><td>StudentName</td><td>StudentPhone</td></tr> </li></ul><ul><li><? </li></ul><ul><li>$conn=mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;&quot;); </li></ul><ul><li>mysql_select_db(&quot;studentdb&quot;,$conn); </li></ul><ul><li>//echo &quot;connected&quot;; </li></ul><ul><li>$result=mysql_query(&quot;SELECT * FROM students&quot;); </li></ul><ul><li>$num_rows=mysql_num_rows($result); </li></ul><ul><li>if ($num_rows >0) </li></ul><ul><li>{ </li></ul><ul><li>while($row=mysql_fetch_array($result)) </li></ul><ul><ul><li>{ </li></ul></ul><ul><li>$sid=$row[&quot;sid&quot;]; </li></ul><ul><li>$sname=$row[&quot;sname&quot;]; </li></ul><ul><li>$sphone=$row[&quot;sphone&quot;]; </li></ul><ul><li>?> </li></ul><ul><li><tr><td><? echo $sid ?></td><td><? echo $sname ?></td><td><? echo “$sphone”; ?> </td> </li></ul><ul><li><? } ?> </li></ul><ul><li></tr></table> </li></ul><ul><li><? </li></ul><ul><li>} </li></ul><ul><li>else { echo &quot;no Records found&quot;; mysql_close($conn); } </li></ul><ul><li>?> </body> </html> </li></ul>
  21. 24. Insert <ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><? </li></ul><ul><li>$conn=mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;&quot;); </li></ul><ul><li>mysql_select_db(&quot;studentdb&quot;,$conn); </li></ul><ul><li>if($submit) </li></ul><ul><li>{ </li></ul><ul><li>$sid=$_POST['sid']; </li></ul><ul><li>$sname=$_POST['sname']; </li></ul><ul><li>$sphone=$_POST['sphone']; </li></ul><ul><li>$sql=&quot;INSERT INTO students(sid,sname,sphone)VALUES('$sid','$sname','$sphone')&quot;; </li></ul><ul><li>mysql_query($sql); </li></ul><ul><li>echo &quot;Record Created&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li><form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;<? echo $PHP_SELF ?>&quot;> </li></ul><ul><li>StudentID:<input type=&quot;text&quot; name=&quot;sid&quot;><br> </li></ul><ul><li>StudentName:<input type=&quot;text&quot; name=&quot;sname&quot;><br> </li></ul><ul><li>Phone:<input type=&quot;text&quot; name=&quot;sphone&quot;><br> </li></ul><ul><li><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Enter Information&quot;> </li></ul><ul><li></form> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  22. 25. A simple PHP class definition <ul><li>Class IntStack </li></ul><ul><li>{ /* a stack with type restricted to int */ </li></ul><ul><li>var $the_stack; </li></ul><ul><li>var $count = 0; </li></ul><ul><li>function push ($intvar) </li></ul><ul><li>{ </li></ul><ul><li>if (is_integer ($intvar)) </li></ul><ul><li>{ </li></ul><ul><li>$this->the_stack[$this->count] = $intvar; //put on stack </li></ul><ul><li>$this->count++; //increment count </li></ul><ul><li>print (“Push of $intvar succeeded.<BR>”); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>print (“Hey, IntStack is for ints only!<BR>”); </li></ul><ul><li>} </li></ul><ul><li>function pop () </li></ul><ul><li>{ </li></ul><ul><li>if ($this->count>0) </li></ul><ul><li>{ </li></ul><ul><li>$this->count--; //decrement count </li></ul><ul><li>$top = $this->the_stack[$this->count]; </li></ul><ul><li>return ($top); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>print (“Hey, the stack is empty!<BR>”); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  23. 26. Exercise: PHP Action File <ul><li><? </li></ul><ul><li>$name= $_POST['Name']; </li></ul><ul><li>$age= $_POST['Age']; </li></ul><ul><li>$country= $_POST['Country']; </li></ul><ul><li>echo &quot;Hello, a person named $name has submitted the answer:<br>&quot;; </li></ul><ul><li>echo &quot;Name is :$name<br>&quot;; </li></ul><ul><li>echo &quot;Age is :$age<br>&quot;; </li></ul><ul><li>echo &quot; Country is :$country<br><br>&quot;; </li></ul><ul><li>if($country == 'Malaysia') </li></ul><ul><li>{ echo &quot; I am Malaysian&quot;; } </li></ul><ul><li>else { echo &quot; I am not Malaysian&quot;; } </li></ul><ul><li>?> </li></ul>save as survey.php
  24. 27. PHP Sessions What is a Session ? Briefly a session is a unique id, to which we can store variables and can access over multiple pages. <ul><li>Implementing a simple session </li></ul><ul><li>Starting a session session_start(); </li></ul><ul><li>2. Register session variables $_SESSION[‘ variable ] =‘VALUE’ ; </li></ul><ul><li>Using session variables $new_var= $_SESSION [‘ variable’ ]; </li></ul><ul><li>Deregistering and destroying sessions </li></ul><ul><li>unset($_SESSION[‘variable’]) ; session_destroy(); </li></ul>
  25. 28. PHP Sessions (Example) <html><body> <? // 1. Start the session session_start(); $name = ‘Ali Hasan’ ; // 2. Register the variable $_SESSION[‘name’]=$name; echo ‘ <b> Page 1 ..</b>’; echo “name value=$name”; ?> <a href=‘Page2.php’> Go to Page 2 </a> </body></html> Page1.php <html><body> <? // 1. Start the session session_start(); $name=$_SESSION[‘name’]; echo ‘ <b> Page 2 ..</b>’; echo “name value=$name”; ?> <a href=‘Page3.php’> Go to Page 3 </a> </body></html> <html><body> <? // 1. Start the session session_start(); $name=$_SESSION[‘name’]; echo ‘ <b> Page 3 ..</b>’; echo “name value=$name”; ?> <a href=‘Page1.php’> Go to Page 1 </a> </body></html> Page2.php Page3.php

×