PHP/MySQL tutorial
Introduction <ul><li>PHP (Hypertext Preprocessor)‏ </li></ul><ul><li>- Open source, server-side, scripting language. </li>...
Tutorial Overview <ul><li>Database (MySQL)‏ </li></ul><ul><li>- DB creation </li></ul><ul><li>- Add/delete tables </li></u...
MySQL & TCD  <ul><li>‘ PuTTY ’ into wilde.cs.tcd.ie on port 22 w/ SSH </li></ul><ul><ul><li>- Authenticate with your TCD u...
Basic MySQL Syntax <ul><li>SHOW DATABASES; </li></ul><ul><li>USE database_name; </li></ul><ul><li>SHOW TABLES; </li></ul><...
Create MySQL Table <ul><li>CREATE TABLE user (name varchar(9) NOT NULL, </li></ul><ul><li>id int(6) NOT NULL, </li></ul><u...
Add/Delete/Update Table <ul><li>INSERT INTO user VALUES (‘bond’, ‘007’); </li></ul><ul><li>DELETE FROM user WHERE id=‘007’...
Query Database <ul><li>SELECT * FROM user; </li></ul><ul><li>SELECT * FROM user WHERE name=‘BOND’; </li></ul><ul><li>SELEC...
PHP User Front-End <ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li>  <?php  </li></ul><ul><li>  $variable=“27100...
PHP Configuration File <ul><li><? </li></ul><ul><li>// configuration parameters </li></ul><ul><li>// database configuratio...
PHP Add to DB Code 1 <ul><li><table cellspacing=&quot;5&quot; cellpadding=&quot;5&quot;> </li></ul><ul><li><form action=&q...
PHP Add to DB Code 2 <ul><li><? </li></ul><ul><li>include(&quot;conf.php&quot;); </li></ul><ul><li>// form submitted so st...
PHP Query Code <ul><li>include(&quot;conf.php&quot;); </li></ul><ul><li>$connection = mysql_connect($host, $user, $pass) o...
PHP Delete Code <ul><li>include(&quot;conf.php&quot;); </li></ul><ul><li>// form not yet submitted, display initial form w...
PHP Update Code 1 <ul><li>$id=$_GET['id'];  </li></ul><ul><li>if (!$submit)‏ </li></ul><ul><li>{ </li></ul><ul><li>$connec...
PHP Update Code 2 <ul><li>include(&quot;conf.php&quot;); </li></ul><ul><li>// form submitted so start processing it </li><...
Summary <ul><li>Create MySQL database(s) with tables as required. </li></ul><ul><li>Create PHP powered webpage for adding,...
Upcoming SlideShare
Loading in...5
×

Ubi comp27nov04

1,353

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,353
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
52
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ubi comp27nov04

  1. 1. PHP/MySQL tutorial
  2. 2. Introduction <ul><li>PHP (Hypertext Preprocessor)‏ </li></ul><ul><li>- Open source, server-side, scripting language. </li></ul><ul><li>- Supports databases such as MySQL and Oracle. </li></ul><ul><li>- http://www.w3schools.com/php/default.asp </li></ul><ul><li>MySQL (Structured Query Language)‏ </li></ul><ul><li>- Open source, speedy, scalable, reliable database technology. </li></ul><ul><li>- http://dev.mysql.com/doc/mysql/en/Tutorial.html </li></ul>
  3. 3. Tutorial Overview <ul><li>Database (MySQL)‏ </li></ul><ul><li>- DB creation </li></ul><ul><li>- Add/delete tables </li></ul><ul><li>- Add/delete/update records </li></ul><ul><li>- View/query records </li></ul><ul><li>Web (PHP)‏ </li></ul><ul><li>- User front-end </li></ul><ul><li>- Add & query code </li></ul><ul><li>- Delete & update code </li></ul>
  4. 4. MySQL & TCD <ul><li>‘ PuTTY ’ into wilde.cs.tcd.ie on port 22 w/ SSH </li></ul><ul><ul><li>- Authenticate with your TCD username and password </li></ul></ul><ul><li>(2) ssh macneil.cs.tcd.ie (password as above)‏ </li></ul><ul><li>(3) Login into your MySQL account </li></ul><ul><li>‘ mysql –uUSERNAME –p’ </li></ul><ul><li>Enter your MySQL username and password </li></ul><ul><li>(4) Use MySQL syntax to create and view table(s), records, etc. </li></ul>
  5. 5. Basic MySQL Syntax <ul><li>SHOW DATABASES; </li></ul><ul><li>USE database_name; </li></ul><ul><li>SHOW TABLES; </li></ul><ul><li>DROP TABLE table_name; </li></ul>
  6. 6. Create MySQL Table <ul><li>CREATE TABLE user (name varchar(9) NOT NULL, </li></ul><ul><li>id int(6) NOT NULL, </li></ul><ul><li>PRIMARY KEY (id), </li></ul><ul><li>UNIQUE (id)‏ </li></ul><ul><li>); </li></ul>
  7. 7. Add/Delete/Update Table <ul><li>INSERT INTO user VALUES (‘bond’, ‘007’); </li></ul><ul><li>DELETE FROM user WHERE id=‘007’; </li></ul><ul><li>UPDATE user SET name=‘BOND’ WHERE id=‘007’; </li></ul>
  8. 8. Query Database <ul><li>SELECT * FROM user; </li></ul><ul><li>SELECT * FROM user WHERE name=‘BOND’; </li></ul><ul><li>SELECT DISTINCT name FROM user; </li></ul><ul><li>SELECT name, id FROM user ORDER BY name; </li></ul>
  9. 9. PHP User Front-End <ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li> <?php </li></ul><ul><li> $variable=“271004&quot;; </li></ul><ul><li> echo $variable; </li></ul><ul><li> ?> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li>Script is executed server side and presented to user via a browser. </li></ul><ul><li>PHP code is rendered as plain HTML. </li></ul>
  10. 10. PHP Configuration File <ul><li><? </li></ul><ul><li>// configuration parameters </li></ul><ul><li>// database configuration </li></ul><ul><li>$host = &quot;macneill.cs.tcd.ie&quot;; </li></ul><ul><li>$user = “username&quot;; </li></ul><ul><li>$pass = “password&quot;; </li></ul><ul><li>$db = “username_db&quot;; </li></ul><ul><li>// default contact person </li></ul><ul><li>$def_contact = “Karl&quot;; </li></ul><ul><li>?> </li></ul><ul><li>Use a securely positioned ‘config’ file to store variables. </li></ul><ul><li>Other PHP pages can link to it and use the variables as their own. </li></ul>
  11. 11. PHP Add to DB Code 1 <ul><li><table cellspacing=&quot;5&quot; cellpadding=&quot;5&quot;> </li></ul><ul><li><form action=&quot;addUpdate.php&quot; method=&quot;POST&quot;> </li></ul><ul><li><tr> </li></ul><ul><li><td valign=&quot;top&quot;><b><font size=&quot;-1&quot;>Title</font></b></td> </li></ul><ul><li><td><textarea name=&quot;title&quot; cols=&quot;40&quot; rows=&quot;2&quot;></textarea></td> </li></ul><ul><li></tr> </li></ul><ul><li><tr> </li></ul><ul><li><td valign=&quot;top&quot;><b><font size=&quot;-1&quot;>Authors</font></b></td> </li></ul><ul><li><td><textarea name=&quot;authors&quot; cols=&quot;40&quot; rows=&quot;2&quot;></textarea></td> </li></ul><ul><li></tr> </li></ul><ul><li>… </li></ul><ul><li><inut type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Add&quot;></td></tr> </li></ul><ul><li></form> </li></ul><ul><li></table> </li></ul>
  12. 12. PHP Add to DB Code 2 <ul><li><? </li></ul><ul><li>include(&quot;conf.php&quot;); </li></ul><ul><li>// form submitted so start processing it </li></ul><ul><li>$title = $_POST[&quot;title&quot;]; </li></ul><ul><li>$authors = $_POST[&quot;authors&quot;]; </li></ul><ul><li>… </li></ul><ul><li>// set up error list array & validate text input fields </li></ul><ul><li>$errorList = array(); </li></ul><ul><li>$count = 0; </li></ul><ul><li>if (!$title) { $errorList[$count] = &quot;Invalid entry: Title&quot;; $count++; } </li></ul><ul><li>// set default value for contact person </li></ul><ul><li>if (!$contact) { $contact = $def_contact; } </li></ul><ul><li>// check for errors & if none found... </li></ul><ul><li>if (sizeof($errorList) == 0)‏ </li></ul><ul><li>{ </li></ul><ul><li>$connection = mysql_connect($host, $user, $pass) or die (&quot;Unable to connect!&quot;); </li></ul><ul><li>mysql_select_db($db) or die (&quot;Unable to select database!&quot;); </li></ul><ul><li>$query = &quot;INSERT INTO papers (title, authors, description, comment, super, bibtex, url, genre) </li></ul><ul><li>VALUES ('$title', '$authors', '$description', '$comment', '$super','$bibtex','$url','$genre')&quot;; </li></ul><ul><li>$result = mysql_query($query) or die (&quot;Error in query: $query. &quot; . mysql_error()); </li></ul><ul><li>echo &quot;<font size=-1>Addition successful.<br><br> <a href=papers.php>Go back to the main page</a> | <a href=http://www.cs.tcd.ie/Karl.Quinn/>home</font>&quot;; </li></ul><ul><li>// close database connection </li></ul><ul><li>mysql_close($connection); </li></ul><ul><li>} </li></ul><ul><li>else {// errors occurred} </li></ul><ul><li>?> </li></ul>
  13. 13. PHP Query Code <ul><li>include(&quot;conf.php&quot;); </li></ul><ul><li>$connection = mysql_connect($host, $user, $pass) or die (); </li></ul><ul><li>mysql_select_db($db) or die (&quot;Unable to select database!&quot;); </li></ul><ul><li>$query = &quot;SELECT * FROM papers&quot;; </li></ul><ul><li>$result = mysql_query($query) or die (&quot;Error in query”); </li></ul><ul><li>?> </li></ul><ul><li><table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; border=&quot;0&quot; width=&quot;622&quot;> </li></ul><ul><li><tr><td bgcolor=&quot;990000&quot;><img src=&quot;images/spacer.gif&quot; alt=&quot;&quot; height=&quot;2&quot;></td></tr> </li></ul><ul><li><? </li></ul><ul><li>// if records present </li></ul><ul><li>if (mysql_num_rows($result) > 0)‏ </li></ul><ul><li>{ </li></ul><ul><li>// iterate through resultset & print title with links to edit and delete scripts </li></ul><ul><li>while($row = mysql_fetch_object($result))‏ </li></ul><ul><li>{ </li></ul><ul><li>?> </li></ul><ul><li><font size=&quot;-2&quot;><a href=&quot;edit.php?id=<? echo $row->id; ?>&quot;>edit/view</a> | <a href=&quot;delete.php?id=<? echo $row->id; ?>&quot;>delete</a></font><p> </li></ul><ul><li><font size=&quot;-1&quot;><b><? echo $row->title; ?></b><br> </li></ul><ul><li><font size=&quot;-1&quot;><b>-<? echo $row->authors; ?></b> </li></ul><ul><li><br><a href=&quot;<? echo $row->url; ?>&quot; target=&quot;_blank&quot;> pdf</a> </li></ul><ul><li><br><br><br> </font> </li></ul><ul><li><table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; border=&quot;0&quot; width=&quot;622&quot;> </li></ul><ul><li><tr><td bgcolor=&quot;990000&quot;><img src=&quot;images/spacer.gif&quot; alt=&quot;“ height=&quot;2&quot;></td></tr> </li></ul><ul><li><? </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>// if no records present </li></ul><ul><li>else{} </li></ul><ul><li>mysql_close($connection); </li></ul><ul><li>?> </li></ul>
  14. 14. PHP Delete Code <ul><li>include(&quot;conf.php&quot;); </li></ul><ul><li>// form not yet submitted, display initial form with values pre-filled </li></ul><ul><li>$id=$_GET['id']; </li></ul><ul><li>{ </li></ul><ul><li>// open database connection </li></ul><ul><li>$connection = mysql_connect($host, $user, $pass) or die (&quot;Unable to connect!&quot;); </li></ul><ul><li>// select database </li></ul><ul><li>mysql_select_db($db) or die (&quot;Unable to select database!&quot;); </li></ul><ul><li>// generate and execute query </li></ul><ul><li>$query = &quot;DELETE FROM papers WHERE id = '$id'&quot;; </li></ul><ul><li>$result = mysql_query($query) or die (&quot;Error in query: $query. &quot; . mysql_error()); </li></ul><ul><li>// close database connection </li></ul><ul><li>mysql_close($connection); </li></ul><ul><li>// print result </li></ul><ul><li>echo &quot;<font size=-1>Deletion successful. <br><br><a href=papers.php>Go back to the main page</a> | <a href=http://www.cs.tcd.ie/Karl.Quinn/>home</font>&quot;; </li></ul><ul><li>} </li></ul>
  15. 15. PHP Update Code 1 <ul><li>$id=$_GET['id']; </li></ul><ul><li>if (!$submit)‏ </li></ul><ul><li>{ </li></ul><ul><li>$connection = mysql_connect($host, $user, $pass) or die (&quot;Unable to connect!&quot;); </li></ul><ul><li>mysql_select_db($db) or die (&quot;Unable to select database!&quot;); </li></ul><ul><li>$query = &quot;SELECT title, authors, description, comment, super, bibtex, url, genre FROM papers WHERE id = '$id'&quot;; </li></ul><ul><li>$result = mysql_query($query) or die (&quot;Error in query: $query. &quot; . mysql_error()) </li></ul><ul><li>if (mysql_num_rows($result) > 0)‏ </li></ul><ul><li>{ </li></ul><ul><li>$row = mysql_fetch_object($result); </li></ul><ul><li>// print form with values pre-filled </li></ul><ul><li>?> </li></ul><ul><li><table cellspacing=&quot;5&quot; cellpadding=&quot;5&quot;> </li></ul><ul><li><form action=&quot;Update.php&quot; method=&quot;POST&quot;> </li></ul><ul><li><input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;<? echo $id; ?>&quot;> </li></ul><ul><li><tr> </li></ul><ul><li><td valign=&quot;top&quot;><b><font size=&quot;-1&quot;>Title</font></b></td> </li></ul><ul><li><td><textarea name=&quot;title&quot; cols=&quot;40&quot; rows=&quot;2&quot;><? echo $row->title; ?></textarea></td> </li></ul><ul><li></tr> </li></ul><ul><li><tr> </li></ul><ul><li><td valign=&quot;top&quot;><b><font size=&quot;-1&quot;>Authors</font></b></td> </li></ul><ul><li><td><textarea name=&quot;authors&quot; cols=&quot;40&quot; rows=&quot;2&quot;><? echo $row >authors; ?></textarea></td> </li></ul><ul><li></tr> </li></ul><ul><li>… </li></ul><ul><li><tr> </li></ul><ul><li><td colspan=2> </li></ul><ul><li><input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Update&quot;></td></tr> </li></ul><ul><li></form> </li></ul><ul><li></table> </li></ul>
  16. 16. PHP Update Code 2 <ul><li>include(&quot;conf.php&quot;); </li></ul><ul><li>// form submitted so start processing it </li></ul><ul><li>$title = $_POST[&quot;title&quot;]; </li></ul><ul><li>$authors = $_POST[&quot;authors&quot;]; </li></ul><ul><li>… </li></ul><ul><li>$id = $_POST[&quot;id&quot;]; </li></ul><ul><li>// set up error list array </li></ul><ul><li>$errorList = array(); </li></ul><ul><li>$count = 0; </li></ul><ul><li>// validate text input fields </li></ul><ul><li>if (!$title) { $errorList[$count] = &quot;Invalid entry: Title&quot;; $count++; } </li></ul><ul><li>if (!$contact) { $contact = $def_contact; } </li></ul><ul><li>// check for errors, if none found... </li></ul><ul><li>if (sizeof($errorList) == 0)‏ </li></ul><ul><li>{ </li></ul><ul><li>$connection = mysql_connect($host, $user, $pass) or die (&quot;Unable to connect!&quot;); </li></ul><ul><li>mysql_select_db($db) or die (&quot;Unable to select database!&quot;); </li></ul><ul><li>$query = &quot;UPDATE papers SET title = '$title', authors = '$authors', description = '$description', comment = '$comment', super = '$super', bibtex = '$bibtex', url = '$url', genre = '$genre' WHERE id = '$id'&quot;; </li></ul><ul><li>$result = mysql_query($query) or die (&quot;Error in query: $query. &quot; . mysql_error()); </li></ul><ul><li>// print result </li></ul><ul><li>echo &quot;<font size=-1>Update successful.<br><br> <a href=papers.php>Go back to the main page</a> | <a href=http://www.cs.tcd.ie/Karl.Quinn/>home</a></font>&quot;; </li></ul><ul><li>// close database connection </li></ul><ul><li>mysql_close($connection); </li></ul><ul><li>} </li></ul><ul><li>else{} </li></ul><ul><li>?> </li></ul>
  17. 17. Summary <ul><li>Create MySQL database(s) with tables as required. </li></ul><ul><li>Create PHP powered webpage for adding, deleting, updating and viewing database information. </li></ul><ul><li>Be aware or security concerns vis-à-vis configuration file contents and database info. </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×