* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download PHP: Part 1
Survey
Document related concepts
Oracle Database wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Ingres (database) wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Concurrency control wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Functional Database Model wikipedia , lookup
Versant Object Database wikipedia , lookup
ContactPoint wikipedia , lookup
Clusterpoint wikipedia , lookup
Transcript
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic PHP Introduction PHP – – – – PHP: Hypertext Preprocessor Originally called “Personal Home Page Tools” Popular server-side scripting technology Open-source – Anyone may view, modify and redistribute source code Supported freely by community Platform independent PHP Basic syntax – – Scripting delimiters <? php ?> Must enclose all script code Variables preceded by $ symbol – – End statements with semicolon Comments – Case-sensitive // for single line and /* */ for multiline Filenames end with .php by convention 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" Outline "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 2 3 4 <!-- Fig. 26.1: first.php --> 5 <!-- Our first PHP script --> Scripting delimiters 6 7 <?php $name = "LunaTic"; 8 9 // declaration Declare variable $name ?> 10 11 <html xmlns = "http://www.w3.org/1999/xhtml"> 12 13 14 <head> <title>A simple PHP document</title>Single-line comment </head> 15 16 17 <body style = "font-size: 2em"> <p> <strong> 18 19 20 <!-- print variable name’s value --> 21 Welcome to PHP, <?php print( "$name" ); ?>! </strong> 22 23 </p> 24 </body> 25 </html> Function print outputs the value of variable $name Variables in PHP Data type Description Whole numbers (i.e., numbers without a decimal point). Real numbers (i.e., numbers containing a decimal point). Text enclosed in either single ('') or double ("") quotes. True or false. Group of elements of the same type. Group of associated data and methods. An external data source. No value. Fig. 26.2 PHP data types. Data types: Concatenation operator int, integer float, double string bool, Boolean array object Resource NULL . (period) example : <?php print( “My name is ” . $name . “and I am from “ . $country ); ?> Operators and Constants Arithmetic operators – Assignment operators Constants – – Syntactical shortcuts: += , *= , -= etc. Before being assigned values, variables have value undef Named values define function See example at: http://sapweb.clarkson.edu/operators.php Arrays Group of related data Elements Name plus braces and index Indices start at zero – count function – array function – Built-in iterators Maintain pointer to element currently referenced reset key next foreach loops Connecting to a Database Interacting with databases – SQL – Structured Query Language Used to manipulate databases Several useful functions mysql_connect mysql_select_db mysql_query mysql_error mysql_fetch_row mysql_close 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Outline 3 4 <!-- Fig. 26.19: database.php --> 5 <!-- Program to query a database and --> 6 <!-- send results to the client. --> 7 8 9 10 11 <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Search Results</title> </head> 12 13 <body style = "font-family: arial, sans-serif" 14 style = "background-color: #F0E68C"> 15 <?php 16 17 Build the select query and assign the string to variable $query. extract( $_POST ); 18 19 // build SELECT query 20 $query = "SELECT " . $select . " Function mysql_connect returns a database handle which represents PHP’s connection to a database. If this connection is not made, function FROM dieBooks"; is called to terminate script execution. 21 22 // Connect to MySQL 23 if ( !( $database = mysql_connect( "localhost", 24 "httpd", "" ) ) ) 25 die( "Could not connect to database" ); 26 27 // open Products database 28 Function)mysql_query if ( !mysql_select_db( "Products", $database ) die( "Could not open Products 29 30 returns an object database" ); containing the result set of the query, which we assign to variable $result. 31 // query Products database 32 if ( !( $result = mysql_query( $query, $database ) ) ) { Function mysql_select_db is called to specify the database to be queried. 33 print( "Could not execute query! <br />" ); 34 die( mysql_error() ); } 35 36 Outline ?> 37 38 <h3 style = "color: blue"> 39 Search Results</h3> 40 41 42 <table border = "1" cellpadding = "3" cellspacing = "2" style = "background-color: #ADD8E6"> 43 44 45 46 47 48 49 50 The for loop iterates through each record in the result set while // fetch each record in result set constructing an XHTML table from for ( $counter = 0; the results. Variable $counter is $row = mysql_fetch_row( $result incremented ); by one for each row $counter++ ){ Function mysql_fetch_row returns an retrieved. array containing the elements of each row in the result set of our query ($result). <?php 51 // build table to display results 52 print( "<tr>" ); 53 foreach ( $row as $key => $value ) 54 print( "<td>$value</td>" ); 55 56 print( "</tr>" ); 57 } 58 59 mysql_close( 60 ?> 61 The foreach loop iterates through the array containing the elements of each row and prints out each element in an $databaseThe ); total number of results are printed to the individual table cell. client. 62 63 </table> 64 65 <br />Your search yielded <strong> 66 <?php print( "$counter" ) ?> results.<br /><br /></strong> 67 68 <h5>Please email comments to <a href = "mailto:[email protected]"> 69 Deitel and Associates, Inc. 70 </a> 71 72 </h5> 73 74 </body> 75 </html> Outline Connecting to a Database