CSCI 305 Introduction to Database Systems

22
<bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="FICTION"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> CSCI 305 Introduction to Database Systems Professor Brian R. King Bucknell University – Computer Science Dept. PHP SELECT * FRO M Students WHERE Grade >= 90 XML

description

Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter - PowerPoint PPT Presentation

Transcript of CSCI 305 Introduction to Database Systems

Page 1: CSCI 305 Introduction to Database Systems

<bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="FICTION"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book></bookstore>

CSCI 305Introduction to Database Systems

Professor Brian R. KingBucknell University – Computer Science Dept.

PHP

SELECT * FROM Students

WHERE Grade >= 90XML

Page 2: CSCI 305 Introduction to Database Systems

Objective• This is designed to teach you some bare

essentials about PHP, and using it to connect to your DB

Page 3: CSCI 305 Introduction to Database Systems

PHP Basics• PHP – Hypertext Preprocessor• Server-side scripting language• Scripts reside between reserved PHP tags– Programmer can embed PHP in the HTML pages– <?php…?>

• Structurally similar to C/C++/Java syntax

Page 4: CSCI 305 Introduction to Database Systems

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

Page 5: CSCI 305 Introduction to Database Systems

PHP echo command• echo is used to output the parameters passed

to it• Typical usage – send data to the client's web-

browser

Page 6: CSCI 305 Introduction to Database Systems

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 “\\”)

<?php$foo = 25; // Numerical variable$bar = “Hello”; // String variable

echo $bar; // Outputs Helloecho $foo,$bar; // Outputs 25Helloecho “5x5=”,$foo; // Outputs 5x5=25echo “5x5=$foo”; // Outputs 5x5=25echo ‘5x5=$foo’; // Outputs 5x5=$foo?>

Page 7: CSCI 305 Introduction to Database Systems

Arithmetic Operations

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

<?php$a=15;$b=30;$total=$a+$b;echo $total;echo “<p><h1>$total</h1>”;// total is 45

?>

Page 8: CSCI 305 Introduction to Database Systems

Concatenation• Use a period to join strings into one.

<?php$string1=“Hello”;$string2=“PHP”;$string3=$string1 . “ ” . $string2;echo $string3;?>

Hello PHP

Page 9: CSCI 305 Introduction to Database Systems

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.

<?php$heading="\"Computer Science\"";echo $heading;?>

“Computer Science”

Page 10: CSCI 305 Introduction to Database Systems

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)

Page 11: CSCI 305 Introduction to Database Systems

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.

Page 12: CSCI 305 Introduction to Database Systems

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)

Page 13: CSCI 305 Introduction to Database Systems

Functions example

<?php // This is a function

function foo($arg_1, $arg_2) { $arg_2 = $arg_1 * $arg_2; return $arg_2;}

$result_1 = foo(12, 3); // Store the function echo $result_1; // Outputs 36echo foo(12, 3); // Outputs 36

?>

Page 14: CSCI 305 Introduction to Database Systems

Include Files• include "opendb.php";• include "closedb.php";• The code in files will be inserted into current

code. – Nice for modularizing and reusability

Page 15: CSCI 305 Introduction to Database Systems

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

Page 16: CSCI 305 Introduction to Database Systems

Example form

welcome.php

Page 17: CSCI 305 Introduction to Database Systems

form.php

<?php if ($_POST["submit"]) echo "<h2>You clicked Submit!</h2>"; else if ($_POST["cancel"]) echo "<h2>You clicked Cancel!</h2>";?>

<form action="form.php" method="post"> <input type="submit" name="submit" value="Submit"> <input type="submit" name="cancel" value="Cancel"></form>

What you see on your browser after clicking Submit

Page 18: CSCI 305 Introduction to Database Systems

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

Page 19: CSCI 305 Introduction to Database Systems

connect.php<?php $user = "brk009"; $password = "……"; $database = "brk009_WWIIShipsDB"; $conn = mysql_connect("db.eg.bucknell.edu",$user,$password); if (!$conn) die("Cound not connect to database"); mysql_select_db($database) or die("Unable to select database");

$result = mysql_query("SHOW TABLES"); if (!$result) die("Query to show tables failed");

Page 20: CSCI 305 Introduction to Database Systems

connect.php (cont.) $num_row = mysql_num_rows($result); echo "<h1>Choose one table:</h1>"; echo "<form action=\"showtable.php\" method=\"POST\">"; echo "<select name=\"table\" size=\"1\" Font size=\"2\">"; for($i = 0; $i < $num_row; $i++) { $tablename = mysql_fetch_row($result); echo "<option value = \"{$tablename[0]}\"

>{$tablename[0]}</option>"; } echo "</select>"; echo "<div><input type=\"submit\" value=\"submit\"></div>"; echo "</form>"; mysql_free_result($result); mysql_close($conn);?>

Page 21: CSCI 305 Introduction to Database Systems

showtable.php<?php $user = "brk009"; $password = "……"; $database = "brk009_WWIIShipsDB"; $table = $_POST["table"]; $conn = mysql_connect("db.eg.bucknell.edu",$user,$password); if (!$conn) die("Cound not connect to database"); mysql_select_db($database) or die("Unable to select

database"); $result = mysql_query("SELECT * FROM {$table}"); if (!$result) die("Query to show tuples from table failed!" .

mysql_error());

Page 22: CSCI 305 Introduction to Database Systems

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