PHP:  Part II Please note that there will be  No homework you can experiment with php and MySQL on your i5 account   Attri...
Road map <ul><li>Php II </li></ul><ul><li>Using MySQL and php on i5 </li></ul>
Working with Strings <ul><li>•  You can concatenate two strings </li></ul><ul><li>together via the . operator. For </li></...
• Mathematical Functions <ul><li>•  PHP provides lots of built-in mathematical  functions. </li></ul><ul><li>For example: ...
• Arrays <ul><li>•  An example array: </li></ul><ul><li>$fruit = array (&quot;apples&quot;, &quot;oranges&quot;, &quot;ban...
•  Associative Arrays <ul><li>•  PHP also supports associative arrays. </li></ul><ul><li>•  With associative arrays, you c...
Reusable PHP Components   <ul><li>• </li></ul><ul><li>•  It’s very easy to create reusable PHP components that </li></ul><...
Reusable PHP Components   (r un )   The PHP Include Statement
Accessing Form Data in PHP ( run ) •  Accessing form data in PHP is very simple. •  Simply query the $_GET variable  (or t...
“ Big Picture”: PHP and MySQL <ul><li>•  PHP Includes built-in support for accessing </li></ul><ul><li>multiple databases,...
Accessing MySQL: Overview <ul><li>•  To Access MySQL from a PHP Script, you generally follow the same four step </li></ul>...
Accessing MySQL: Overview Step 1: Connect to MySQL <ul><li>•  Before you can retrieve data from a MySQL table, you must fi...
Accessing MySQL: Overview Step 1: Connect to MySQL <ul><li>Connection Errors </li></ul><ul><li>•  The mysql_connect functi...
The @ Error Suppressor <ul><li>•  Suppose we have the following code </li></ul><ul><li>with an invalid user name/password:...
• Connection Errors <ul><li>•  if mysql_connect returns false, you know something has gone </li></ul><ul><li>wrong. </li><...
Side Note: the die() function <ul><li>•  The die() function will output a specific </li></ul><ul><li>error message, and th...
  Step 2: Select a Database  <ul><li>•  This part is easy. To select a database, use the </li></ul><ul><li>mysql_select_db...
Step 3: Issue a SQL Statement <ul><li>•  To issue a SQL statement, use the  mysql_query </li></ul><ul><li>function. For ex...
Step 4: Iterate through the SQL Result Set <ul><li>•  If you have issued a SELECT statement, you want to iterate through a...
Case study #1 ( run ) Step 1: Connect Check for errors Step 2: select DB Check for errors
Case study #1 continued … Step 3: issue SQL statement Step 4: Iterate through result set Check for errors
Accessing MySQL on i5.nyu.edu <ul><li>To use MySQL on i5.nyu.edu: </li></ul><ul><li>Logon to i5.nyu.edu.  </li></ul><ul><l...
<ul><li>Here are some commands to try on i5.nyu.edu: </li></ul><ul><li>$  mysql -u <netid> <netid> </li></ul><ul><li>mysql...
<ul><li>To see review of Mysql commands: </li></ul><ul><li>http://cs.nyu.edu/courses/spring04/V22.0380-001/ MysqlSummary ....
Here are some notes about writing and running PhP with MySQL on i5.nyu.edu: <ul><li>Your_php_script must be located in  pu...
Lets look at the following Examples based on these 2 sql scripts (creating 2 tables) <ul><li>recordings_script.sql  (creat...
composers_script.sql <ul><li>DROP TABLE IF EXISTS composers;  </li></ul><ul><li>CREATE TABLE composers ( composer_name var...
recordings_script.sql <ul><li>DROP TABLE IF EXISTS recordings;  </li></ul><ul><li>CREATE TABLE recordings ( title varchar(...
<ul><li>#!/usr/local/bin/php </li></ul><ul><li>  </li></ul><ul><li>// example using php and mysql on i5.nyu.edu </li></ul>...
<ul><li>/* … continued….  see how many records are returned */  </li></ul><ul><li>$num_rows = mysql_num_rows($result); </l...
Where to get more info. On PHP <ul><li>•  When you get stuck, there are three places </li></ul><ul><li>to check first: </l...
Acknowledgements <ul><li>•  Information contained in these slides is based </li></ul><ul><ul><li>on these sources: </li></...
Upcoming SlideShare
Loading in …5
×

Download It

2,503 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,503
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
75
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Download It

  1. 1. PHP: Part II Please note that there will be No homework you can experiment with php and MySQL on your i5 account Attribution These slides are based on: -Ethan Cerami: http://ecerami.com/mysql/
  2. 2. Road map <ul><li>Php II </li></ul><ul><li>Using MySQL and php on i5 </li></ul>
  3. 3. Working with Strings <ul><li>• You can concatenate two strings </li></ul><ul><li>together via the . operator. For </li></ul><ul><li>example: </li></ul><ul><ul><li>$sequence1 = &quot;cagtaagtga&quot;; </li></ul></ul><ul><ul><li>$sequence2 = &quot;agatgtgcgt&quot;; </li></ul></ul><ul><ul><li>$sequence3 = $sequence1 . $sequence2; </li></ul></ul><ul><ul><li>Working with Strings </li></ul></ul><ul><li>• PHP provides lots of built-in functions for </li></ul><ul><li>manipulating strings. For example: </li></ul><ul><ul><li>– strtolower -- Make a string lowercase </li></ul></ul><ul><ul><li>– strtoupper -- Make a string uppercase </li></ul></ul><ul><ul><li>– trim -- Strip whitespace from the beginning and end of a string </li></ul></ul><ul><ul><li>– strlen -- Get string length </li></ul></ul><ul><ul><li>– strrev -- Reverse a string </li></ul></ul><ul><li>• </li></ul><ul><li>Go to: </li></ul><ul><li>http://us4.php.net/manual/en/ref.strings.php </li></ul><ul><li>for a full list of string manipulation functions. </li></ul>
  4. 4. • Mathematical Functions <ul><li>• PHP provides lots of built-in mathematical functions. </li></ul><ul><li>For example: </li></ul><ul><ul><li>– abs -- Absolute value </li></ul></ul><ul><ul><li>– cos -- Cosine </li></ul></ul><ul><ul><li>– decbin -- Decimal to binary </li></ul></ul><ul><ul><li>– dechex -- Decimal to hexadecimal </li></ul></ul><ul><ul><li>– decoct -- Decimal to octal </li></ul></ul><ul><ul><li>– deg2rad -- Converts the number in degrees to the radian equivalent </li></ul></ul><ul><ul><li>– floor -- Round fractions down </li></ul></ul><ul><ul><li>– log10 -- Base-10 logarithm </li></ul></ul><ul><ul><li>– log -- Natural logarithm </li></ul></ul><ul><ul><li>– max -- Find highest value </li></ul></ul><ul><ul><li>– min -- Find lowest value </li></ul></ul><ul><ul><li>– pow -- Exponential expression </li></ul></ul><ul><ul><li>– rand -- Generate a random integer </li></ul></ul><ul><ul><li>– sin -- Sine </li></ul></ul><ul><ul><li>– sqrt -- Square root </li></ul></ul><ul><ul><li>– tan -- Tangent </li></ul></ul>
  5. 5. • Arrays <ul><li>• An example array: </li></ul><ul><li>$fruit = array (&quot;apples&quot;, &quot;oranges&quot;, &quot;bananas&quot;); </li></ul><ul><li>echo &quot;<P>Fruit: $fruit[1]&quot;; // This prints Oranges </li></ul><ul><li>• Note: just like ordinary variables, each space </li></ul><ul><li>in an array can contain any type of value. </li></ul><ul><li>• For example, this is considered legal in PHP: </li></ul><ul><li>$numbers = array (&quot;zero&quot;, 1, &quot;two&quot;); </li></ul>
  6. 6. • Associative Arrays <ul><li>• PHP also supports associative arrays. </li></ul><ul><li>• With associative arrays, you can use strings as </li></ul><ul><li>indexes. </li></ul><ul><li>• It's called associative because it associates values </li></ul><ul><li>with meaningful indices. </li></ul><ul><li>• </li></ul><ul><li>You can think of an associative array as a look-up </li></ul><ul><li>table. </li></ul><ul><li>• For example, you might have an associative array of </li></ul><ul><li>organisms which maps common names to species </li></ul><ul><li>names. </li></ul><ul><li>• If you request the element with the key “human”, </li></ul><ul><li>you get back the value “Homo Sapiens”. </li></ul>
  7. 7. Reusable PHP Components <ul><li>• </li></ul><ul><li>• It’s very easy to create reusable PHP components that </li></ul><ul><li>can be used by multiple web pages. </li></ul><ul><li>• For example, your web site might include two components: </li></ul><ul><li>header.php and footer.php . </li></ul><ul><li>• All pages in you web site can include these two </li></ul><ul><li>components. </li></ul><ul><li>• If you want to update the header, just update the header </li></ul><ul><li>component, and the change automatically appears on all </li></ul><ul><li>pages. </li></ul><ul><li>• Main advantages of reusable components: </li></ul><ul><ul><li>– much easier to maintain </li></ul></ul><ul><ul><li>– more modular web site structure </li></ul></ul>
  8. 8. Reusable PHP Components (r un ) The PHP Include Statement
  9. 9. Accessing Form Data in PHP ( run ) • Accessing form data in PHP is very simple. • Simply query the $_GET variable (or the $_REQUEST variable) for the form parameter you want.
  10. 10. “ Big Picture”: PHP and MySQL <ul><li>• PHP Includes built-in support for accessing </li></ul><ul><li>multiple databases, including Oracle, Microsoft </li></ul><ul><li>SQL Server, MySQL, etc. </li></ul><ul><ul><li>• PHP support for MySQL is both simple and </li></ul></ul><ul><li>comprehensive. </li></ul><ul><ul><li>• PHP support for MySQL is fully documented </li></ul></ul><ul><li>here: </li></ul><ul><ul><li>– http://us2.php.net/manual/en/ref.mysql.php </li></ul></ul><ul><ul><li>– (Official Reference Page) </li></ul></ul>
  11. 11. Accessing MySQL: Overview <ul><li>• To Access MySQL from a PHP Script, you generally follow the same four step </li></ul><ul><li>process : </li></ul><ul><ul><li>– Step 1: Make a connection to the MySQL database server. </li></ul></ul><ul><ul><li>• this requires that you specify the location of the database, plus a user </li></ul></ul><ul><ul><li>name and password. </li></ul></ul><ul><ul><li>– Step 2: Select the database within MySQL that you want to work with. </li></ul></ul><ul><ul><li>• for example, you may want to select the “ensmartdb” database we </li></ul></ul><ul><ul><li>created in Class #1. </li></ul></ul><ul><ul><li>– Step 3: Issue a SQL Statement </li></ul></ul><ul><ul><li>• this could be any valid SQL statement, e.g. SELECT, INSERT, UPDATE, </li></ul></ul><ul><ul><li>DELETE. </li></ul></ul><ul><ul><li>– Step 4: If you are issuing a SELECT statement, iterate through the result </li></ul></ul><ul><ul><li>set and extract each record. </li></ul></ul><ul><ul><li>• for example, extract specific fields in each record, and output as HTML. </li></ul></ul><ul><ul><li>• At each step, check for specific error conditions. If an error occurs, fail </li></ul></ul><ul><li>gracefully and inform the user of the problem. </li></ul>
  12. 12. Accessing MySQL: Overview Step 1: Connect to MySQL <ul><li>• Before you can retrieve data from a MySQL table, you must first </li></ul><ul><li>connect to the MySQL database server. </li></ul><ul><li>• To connect, you use the msql_connect function. </li></ul><ul><li>mysql_connect (address, user_name, password); </li></ul><ul><li>– Here, address is the IP address or host name of the computer where </li></ul><ul><li>the MySQL server is actually running. </li></ul><ul><li>– If you are connecting to a MySQL server running on your local </li></ul><ul><li>machine, the address should be set to “ localhost ”. </li></ul><ul><li>• For example: </li></ul><ul><li>$connection = mysql_connect(&quot;localhost&quot;, ”username&quot;, ”password&quot;); </li></ul>
  13. 13. Accessing MySQL: Overview Step 1: Connect to MySQL <ul><li>Connection Errors </li></ul><ul><li>• The mysql_connect function is not guaranteed to work. For </li></ul><ul><li>example: </li></ul><ul><ul><li>– the database server might be down, or </li></ul></ul><ul><ul><li>– the database server might be on a different machine, and the </li></ul></ul><ul><ul><li>network connection is down. </li></ul></ul><ul><li>• It is important to test for these types of errors so that you can </li></ul><ul><li>report meaningful error messages to your users. </li></ul><ul><ul><li>• If the connection attempt succeeds , the mysql_connect function </li></ul></ul><ul><ul><li>will return a number that identifies the connection to the </li></ul></ul><ul><ul><li>database. </li></ul></ul><ul><ul><li>• If the connection attempt fails, the mysql_connect function will </li></ul></ul><ul><ul><li>return false . </li></ul></ul><ul><li>• Therefore, if mysql_connect returns false , you know something has gone awry. </li></ul>
  14. 14. The @ Error Suppressor <ul><li>• Suppose we have the following code </li></ul><ul><li>with an invalid user name/password: </li></ul><ul><ul><li>$connection = mysql_connect (&quot;localhost&quot;, ”demo&quot;, ”demo&quot;); </li></ul></ul><ul><li>• </li></ul><ul><li>By default, the mysql_connect will </li></ul><ul><li>display a detailed error message </li></ul><ul><li>directly to the user </li></ul><ul><li>• However, you may want to present a more user-friendly error message to your </li></ul><ul><li>users. </li></ul><ul><li>• To suppress error messages from automatically appearing, use the @ error </li></ul><ul><li>suppression operator directly before the function call. </li></ul><ul><li>• For example: </li></ul><ul><li>$connection = @mysql_connect(&quot;localhost&quot;, ”demo&quot;, ”demo&quot;); </li></ul><ul><li>• Then, explicitly check the return value of mysql_connect (see next slide). </li></ul><ul><li>Connecti </li></ul>
  15. 15. • Connection Errors <ul><li>• if mysql_connect returns false, you know something has gone </li></ul><ul><li>wrong. </li></ul><ul><li>• To determine exactly what went wrong, you can use the </li></ul><ul><li>mysql_error function. </li></ul><ul><li>• </li></ul><ul><li>You can therefore create your own custom error page. </li></ul><ul><li>• For example: </li></ul><ul><li>// Connect to the Database. </li></ul><ul><li>// The @ character will suppress default error messages. </li></ul><ul><li>$connection = @mysql_connect(&quot;localhost&quot;, ”guest&quot;, ”guest&quot;); </li></ul><ul><li>// If we fail to connect, display an error message and exit. </li></ul><ul><li>if ($connection == false) { </li></ul><ul><ul><ul><li>echo (&quot;Unable to connect to the database server.&quot;); </li></ul></ul></ul><ul><ul><ul><li>die (&quot;<P>MySQL Error: &quot; . mysql_error()); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  16. 16. Side Note: the die() function <ul><li>• The die() function will output a specific </li></ul><ul><li>error message, and then stop processing </li></ul><ul><li>of the PHP script. </li></ul><ul><li>– Any remaining PHP code is simply ignored. </li></ul><ul><li>• Very useful for reporting error </li></ul><ul><li>messages to the user. </li></ul>
  17. 17. Step 2: Select a Database <ul><li>• This part is easy. To select a database, use the </li></ul><ul><li>mysql_select_db function. </li></ul><ul><li>• Just like mysql_connect, mysql_select_db will return false if an </li></ul><ul><li>error occurs. </li></ul><ul><li>• Sample Code: </li></ul><ul><li>// Select the correct database. </li></ul><ul><li>// The @ character will suppress default error messages. </li></ul><ul><li>$selection = @mysql_select_db(&quot;ensmartdb&quot;); </li></ul><ul><li>// If we fail to select, display an error message and exit. </li></ul><ul><ul><li>if ($selection == false) { </li></ul></ul><ul><ul><li>echo (&quot;Unable to select database.&quot;); </li></ul></ul><ul><ul><li>die (&quot;<P>MySQL Error: &quot; . mysql_error()); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  18. 18. Step 3: Issue a SQL Statement <ul><li>• To issue a SQL statement, use the mysql_query </li></ul><ul><li>function. For example: </li></ul><ul><li>$result = mysql_query (&quot;SELECT * from organism&quot;); </li></ul><ul><li>• As usual, if an error occurs, mysql_query will return </li></ul><ul><li>false </li></ul>
  19. 19. Step 4: Iterate through the SQL Result Set <ul><li>• If you have issued a SELECT statement, you want to iterate through all </li></ul><ul><li>the records in your result set. </li></ul><ul><li>• </li></ul><ul><li>To do so, use the mysql_fetch_array function along with a while loop. </li></ul><ul><ul><li>• Each time you call mysql_fetch_array, you get back a row variable </li></ul></ul><ul><ul><li>corresponding to a single record in the result set. </li></ul></ul><ul><li>• This row variable is </li></ul><ul><li>an associative array, which </li></ul><ul><li>will contain all the field </li></ul><ul><li>data for a record. </li></ul><ul><li>• Note: You can access array </li></ul><ul><li>elements by key strings or </li></ul><ul><li>by index value. </li></ul>
  20. 20. Case study #1 ( run ) Step 1: Connect Check for errors Step 2: select DB Check for errors
  21. 21. Case study #1 continued … Step 3: issue SQL statement Step 4: Iterate through result set Check for errors
  22. 22. Accessing MySQL on i5.nyu.edu <ul><li>To use MySQL on i5.nyu.edu: </li></ul><ul><li>Logon to i5.nyu.edu. </li></ul><ul><li>Then connect to your own database using </li></ul><ul><ul><li>mysql -u <netid> </li></ul></ul><ul><li>Once you are at the mysql prompt, </li></ul><ul><ul><li>then you need to type: </li></ul></ul><ul><ul><li> use <netid> to continue </li></ul></ul><ul><li>Here are some notes about writing and running MySQL scripts and using MySQL on i5.nyu.edu: </li></ul><ul><li>you may place your MySQL scripts in your home directory </li></ul><ul><li>use Pico or vi to edit the script </li></ul><ul><li>use &quot; ; &quot; at the end of a script line or command </li></ul><ul><li>save your script file with an &quot;.sql &quot; suffix </li></ul>
  23. 23. <ul><li>Here are some commands to try on i5.nyu.edu: </li></ul><ul><li>$ mysql -u <netid> <netid> </li></ul><ul><li>mysql> use <netid> </li></ul><ul><li>mysql> create table book ( </li></ul><ul><li>  </li></ul><ul><li>-> title varchar(32), </li></ul><ul><li>  </li></ul><ul><li>-> author varchar(64) </li></ul><ul><li>  </li></ul><ul><li>-> ); </li></ul><ul><li>[Result should be: Query OK. 0 rows affected.] </li></ul><ul><li>mysql> insert into book values ('Pride and Prejudice', 'Jane Austen'); </li></ul><ul><li>[Result should be: Query OK. 1 row affected ] </li></ul><ul><li>mysql> select * from book; </li></ul><ul><li>[Result should show the name and author and 1 row in set ] </li></ul><ul><li>mysql> exit </li></ul><ul><li>[Result should show Bye -> and return you to the Unix command line prompt.] </li></ul><ul><li>  </li></ul>
  24. 24. <ul><li>To see review of Mysql commands: </li></ul><ul><li>http://cs.nyu.edu/courses/spring04/V22.0380-001/ MysqlSummary . htm </li></ul>
  25. 25. Here are some notes about writing and running PhP with MySQL on i5.nyu.edu: <ul><li>Your_php_script must be located in public_html/cgi-bin </li></ul><ul><li>The first line of your PHP script has the directive: #!/usr/local/bin/php </li></ul><ul><li>Use chmod 755 <filename.php> in order for it to be world-executable. </li></ul><ul><li>Your database name is the same as your netid. </li></ul>
  26. 26. Lets look at the following Examples based on these 2 sql scripts (creating 2 tables) <ul><li>recordings_script.sql (creating 1 table) </li></ul><ul><li>composers_script.sql (creating 1 table) </li></ul><ul><li>php_mysql6.php script (run sql queries) </li></ul><ul><li>HTML Form: http://i5.nyu.edu/%7Edse7916/ php _mysql6.html </li></ul><ul><li>Full example on this page (implantation for i5 accounts): </li></ul><ul><ul><ul><li>http://cs.nyu.edu/courses/spring04/V22.0380-001/ PhpMysqlTogetherSummary . htm </li></ul></ul></ul>
  27. 27. composers_script.sql <ul><li>DROP TABLE IF EXISTS composers; </li></ul><ul><li>CREATE TABLE composers ( composer_name varchar(32) NOT NULL, century integer(2) DEFAULT 0, origin varchar(15), PRIMARY KEY(composer_name)); </li></ul><ul><li>INSERT INTO composers VALUES (&quot;Beethoven&quot;, 19, &quot;German&quot;); INSERT INTO composers VALUES (&quot;Handel&quot;, 18, &quot;British&quot;); INSERT INTO composers VALUES (&quot;Verdi&quot;, 19, &quot;Italian&quot;); INSERT INTO composers VALUES (&quot;Tchaikovsky&quot;, 19, &quot;Russian&quot;); </li></ul><ul><li>INSERT INTO composers VALUES (&quot;Copland&quot;, 20, &quot;American&quot;); </li></ul><ul><li>INSERT INTO composers VALUES (&quot;Bach&quot;, 18, &quot;German&quot;); INSERT INTO composers VALUES (&quot;Bizet&quot;, 19, &quot;French&quot;); </li></ul>
  28. 28. recordings_script.sql <ul><li>DROP TABLE IF EXISTS recordings; </li></ul><ul><li>CREATE TABLE recordings ( title varchar(32) NOT NULL, composer_name varchar(32) NOT NULL, medium varchar(5), price decimal(6,2), year_recorded int(4), music_type varchar(10), PRIMARY KEY(title), FOREIGN KEY(composer_name) REFERENCES composers (composer_name)); </li></ul><ul><li>INSERT INTO recordings VALUES (&quot;Symphony #9&quot;,&quot;Beethoven&quot;,&quot;CD&quot;,15.99,1998,&quot;Orchestral&quot;); </li></ul><ul><li>INSERT INTO recordings VALUES (&quot;Sleeping Beauty&quot;,&quot;Tchaikovsky&quot;,&quot;DVD&quot;,34.99, 1992, &quot;Ballet&quot;); </li></ul><ul><li>INSERT INTO recordings VALUES (&quot;La Traviata&quot;,&quot;Verdi&quot;,&quot;Video&quot;,29.99, 2001, &quot;Opera&quot;); INSERT INTO recordings VALUES (&quot;Carmen&quot;,&quot;Bizet&quot;,&quot;Video&quot;,34.99, 1982, &quot;Opera&quot;); </li></ul><ul><li>INSERT INTO recordings VALUES (&quot;Symphony #4&quot;,&quot;Tchaikovsky&quot;,&quot;CD&quot;, 10.99, 1998, &quot;Orchestral&quot;); INSERT INTO recordings VALUES (&quot;Tocatta & Fugue&quot;,&quot;Bach&quot;,&quot;CD&quot;, 21.95,1997,&quot;Orchestral&quot;); </li></ul><ul><li>INSERT INTO recordings VALUES (&quot;Fidelio&quot;,&quot;Beethoven&quot;,&quot;DVD&quot;,49.99, 1991,&quot;Opera&quot;); INSERT INTO recordings VALUES (&quot;Appalachian Spring&quot;,&quot;Copland&quot;,&quot;Video&quot;,19.99, 2000,&quot;Orchestral&quot;); </li></ul><ul><li>INSERT INTO recordings VALUES (&quot;Il Trovatore&quot;,&quot;Verdi&quot;,&quot;DVD&quot;,44.99, 1990, &quot;Opera&quot;); INSERT INTO recordings VALUES (&quot;Swan Lake&quot;,&quot;Tchaikovsky&quot;,&quot;Video&quot;,14.99,1982,&quot;Ballet&quot;); </li></ul>
  29. 29. <ul><li>#!/usr/local/bin/php </li></ul><ul><li>  </li></ul><ul><li>// example using php and mysql on i5.nyu.edu </li></ul><ul><li><HTML> </li></ul><ul><li><head> </li></ul><ul><li><title>PHP: Sample Form </title> </li></ul><ul><li></head> </li></ul><ul><li><body bgcolor = lightblue> </li></ul><ul><li>  </li></ul><ul><li><h2>Composers on File </h2> </li></ul><ul><li><p> </li></ul><ul><li><?php </li></ul><ul><li>/* Connecting, selecting database on i5.nyu.edu*/ </li></ul><ul><li>$link = mysql_connect(&quot;&quot;, “your_netid&quot;, &quot;&quot;) or die(&quot;Could not connect : &quot; . mysql_error()); </li></ul><ul><li>/* echo &quot;Connected successfully&quot;; -- for testing purposes */ </li></ul><ul><li>mysql_select_db(&quot; your_netid &quot;) or die(&quot;Could not select database&quot;); </li></ul><ul><li>  </li></ul><ul><li>/* setting a variable for the nation of origin on the HTML form */ </li></ul><ul><li>$country = $_POST['country']; </li></ul><ul><li>  </li></ul><ul><li>/* Performing SQL query */ </li></ul><ul><li>$query = </li></ul><ul><li>&quot;SELECT a.title, a.composer_name,a.music_type </li></ul><ul><li>FROM recordings a, composers b </li></ul><ul><li>WHERE a.composer_name = b.composer_name AND origin='$country' </li></ul><ul><li>ORDER BY a.title &quot;; </li></ul><ul><li>  </li></ul><ul><li>$result = mysql_query($query) or die(&quot;Query failed : &quot; . mysql_error()); </li></ul><ul><li>  </li></ul><ul><li>(… continued on next page….) </li></ul>
  30. 30. <ul><li>/* … continued…. see how many records are returned */ </li></ul><ul><li>$num_rows = mysql_num_rows($result); </li></ul><ul><li>  </li></ul><ul><li>if ($num_rows > 0) { </li></ul><ul><li>/* Printing results in HTML */ </li></ul><ul><li>echo &quot;<table> &quot;; </li></ul><ul><li>while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { </li></ul><ul><li>echo &quot; <tr> &quot;; </li></ul><ul><li>foreach ($line as $col_value) { </li></ul><ul><li>echo &quot; <td>$col_value</td> &quot;; </li></ul><ul><li>} // end of foreach </li></ul><ul><li>echo &quot; </tr> &quot;; </li></ul><ul><li>} // end of while </li></ul><ul><li>echo &quot;</table> &quot;; </li></ul><ul><li>echo &quot;<i> $num_rows records match this query. </i>&quot;; </li></ul><ul><li>} else { </li></ul><ul><li>echo &quot;There were no results that match this query. Please try again. &quot;; </li></ul><ul><li>} </li></ul><ul><li>  </li></ul><ul><li>/* Free resultset */ </li></ul><ul><li>mysql_free_result($result); </li></ul><ul><li>/* Closing connection */ </li></ul><ul><li>mysql_close($link); </li></ul><ul><li>?> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li>  </li></ul><ul><li>  </li></ul>
  31. 31. Where to get more info. On PHP <ul><li>• When you get stuck, there are three places </li></ul><ul><li>to check first: </li></ul><ul><li>– Check the PHP Manual at: </li></ul><ul><li>http://www.php.net/docs.php </li></ul><ul><li>– Check the PHP FAQ at: </li></ul><ul><li>http://www.php.net/FAQ.php </li></ul><ul><li>– If you want information about a specific function , </li></ul><ul><li>go to: </li></ul><ul><li>http://www.php.net/function _name. For </li></ul><ul><li>example: </li></ul><ul><li>• For information about echo, go to: http://php.net/echo </li></ul><ul><li>• For information about phpinfo, go to: </li></ul><ul><li>http://php.net/phpinfo </li></ul>
  32. 32. Acknowledgements <ul><li>• Information contained in these slides is based </li></ul><ul><ul><li>on these sources: </li></ul></ul><ul><ul><ul><li>– W3 Schools PHP Tutorial. </li></ul></ul></ul><ul><ul><ul><li>http://www.w3schools.com/php/default.asp </li></ul></ul></ul><ul><ul><ul><li>– Kevin Yank, “Building a Database-Driven Web Site </li></ul></ul></ul><ul><ul><ul><li>Using PHP and MySQL” (SitePoint). </li></ul></ul></ul><ul><ul><ul><li>http://dev.mysql.com/techresources/ </li></ul></ul></ul><ul><ul><ul><li>articles/ddws/ </li></ul></ul></ul><ul><ul><ul><li>– Rasmus Lerdorf, “PHP Pocket Reference” (O’Reilly </li></ul></ul></ul><ul><ul><ul><li>& Associates). </li></ul></ul></ul><ul><ul><ul><li>http://www.oreilly.com/catalog/phppr/chapter/php </li></ul></ul></ul><ul><ul><ul><li>_pkt.html </li></ul></ul></ul><ul><ul><ul><li>Deena Engel course webiste: </li></ul></ul></ul><ul><ul><ul><li>http://www.cs.nyu.edu/courses/spring04/V22.0380-001/PhpSummary.htm </li></ul></ul></ul>

×