Presentation is loading. Please wait.

Presentation is loading. Please wait.

Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 Learning XML Erik T. Ray 2003 39.95 CSCI 305 Introduction to Database.

Similar presentations


Presentation on theme: "Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 Learning XML Erik T. Ray 2003 39.95 CSCI 305 Introduction to Database."— Presentation transcript:

1 Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 Learning XML Erik T. Ray 2003 39.95 CSCI 305 Introduction to Database Systems Professor Brian R. King Bucknell University – Computer Science Dept. PHP SELECT * FROM Students WHERE Grade >= 90 XML

2 Objective This is designed to teach you some bare essentials about PHP, and using it to connect to your DB

3 PHP Basics PHP – Hypertext Preprocessor Server-side scripting language Scripts reside between reserved PHP tags – Programmer can embed PHP in the HTML pages – Structurally similar to C/C++/Java syntax

4 PHP Basics Comments: – // –#–# – /* */ Variables: – All variables begin with $ – Case sensitive Type: – PHP is loosely typed. You do not need to declare variables before you add a value to it. PHP determines the type, depending on its initial value

5 PHP echo command echo is used to output the parameters passed to it Typical usage – send data to the client's web- browser

6 Echo example Notice how echo ‘5x5=$foo’ outputs $foo rather than replacing it with 25 Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP This is true for both variables and character escape-sequences (such as “\n” or “\\”)

7 Arithmetic Operations $a - $b // subtraction $a * $b// multiplication $a / $b// division $a += 5// $a = $a+5 Also works for *= and /=

8 Concatenation Use a period to join strings into one. Hello PHP

9 Escaping the Character If the string has a set of double quotation marks that must remain visible, use the \ [backslash] before the quotation marks to ignore and display them. “Computer Science”

10 Control Structures if / else if / else while / do..while / for – foreach exists, but syntax is different switch / case / break / default boolean expressions All of the above are formed just like Java (same comparison and logical operators)

11 Arrays Three types: – Numeric array – An array with a numeric index – Associative array – Each ID key is associated with a value – Multidimensional array Dealing with arrays is easy. However, the library of functions for arrays is large.

12 Functions Functions MUST be defined before then can be called Function headers are of the format – Note that no return type is specified Unlike variables, function names are not case sensitive (foo(…) == Foo(…) == FoO(…)) function functionName($arg_1, $arg_2, …, $arg_n)

13 Functions example

14 Include Files include "opendb.php"; include "closedb.php"; The code in files will be inserted into current code. – Nice for modularizing and reusability

15 PHP Forms and User Input PHP $_GET and $_POST variables are used to retrieve information from forms, like user input NOTE: Any form element in an HTML page will automatically be available to your PHP scripts

16 Example form welcome.php

17 form.php What you see on your browser after clicking Submit

18 MySQL connection Best way is to just show by example…

19 connect.php { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/14/4392695/slides/slide_19.jpg", "name": "connect.php

20 connect.php (cont.) $num_row = mysql_num_rows($result); echo " Choose one table: "; echo " "; for($i = 0; $i < $num_row; $i++) { $tablename = mysql_fetch_row($result); echo " {$tablename[0]} "; } echo " "; mysql_free_result($result); mysql_close($conn); ?> $num_row = mysql_num_rows($result); echo " Choose one table: "; echo " "; for($i = 0; $i < $num_row; $i++) { $tablename = mysql_fetch_row($result); echo " {$tablename[0]} "; } echo " "; mysql_free_result($result); mysql_close($conn); ?>

21 showtable.php { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/14/4392695/slides/slide_21.jpg", "name": "showtable.php

22 showtable.php (cont.) $fields_num = mysql_num_fields($result); echo " Table: {$table} "; echo " "; // Print headers for($i = 0; $i < $fields_num; $i++) { $field = mysql_fetch_field($result); echo " {$field->name} "; } echo " \n"; // Print data while($row = mysql_fetch_row($result)) { echo " "; foreach($row as $cell) echo " $cell "; echo " \n"; } mysql_free_result($result); mysql_close($conn); ?> $fields_num = mysql_num_fields($result); echo " Table: {$table} "; echo " "; // Print headers for($i = 0; $i < $fields_num; $i++) { $field = mysql_fetch_field($result); echo " {$field->name} "; } echo " \n"; // Print data while($row = mysql_fetch_row($result)) { echo " "; foreach($row as $cell) echo " $cell "; echo " \n"; } mysql_free_result($result); mysql_close($conn); ?>


Download ppt "Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 Learning XML Erik T. Ray 2003 39.95 CSCI 305 Introduction to Database."

Similar presentations


Ads by Google