PHP - Getting good with MySQL part II


Published on

Getting good with MySQL part 2

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Packets contained data address, error control and sequencing information.
    TCP : ensure that messages were properly routed from sender to receiver
    IP : Creating “network of networks”. Get different networks to communicate
  • RED LEVEL : Internet "Backbone". It is the main highest-traffic network,interconnects the routers of the second level.
    Internet Routers — shown as round black "nodes" in this diagram — examine the destinations of individual Internet data packets,determine the best route to use in forwarding the data to its final destination.
    BLUE LEVEL : large "Tier-1" service providers. They purchase "backbone" bandwidth from the major communications carriers and resell it to smaller service providers, universities, and corporations.
    GREEN LEVEL : composed of smaller (non-Tier-1) service providers, universities, and corporations. Purchase bandwidth from Tier-1 providers and interconnect the individual machines within their organizations into a "Local Area Network" (LAN).
    BLACK LEVEL : composed of the connections to individual machines. This is often called the "Last Mile" of the Internet and ranges from dial-up modems, to ISDN, to DSL, to Cable Modem, to local Ethernet segments.
  • Problem : interruption in the connection of two-tiered architecture could put the entire data transaction in risk of loss.
  • PHP - Getting good with MySQL part II

    1. 1. 1 Internet Programming Connecting PHP to a Database
    2. 2. 2 Objectives To construct programs that interact with MySQL database.
    3. 3. TAB1033 - Internet Programming 3 How they are connected?
    4. 4. 4 MySQL A Relational Database with a license, allowing users to use it cost-free for most non-commercial purposes. A database choice for PHP developers. Conceived as fast and flexible DBMS.
    5. 5. 5 Getting a Connection Connect to any server that have permission to use. The mysql_connect function arranges the communication link between MySQL and PHP. The mysql_connect() function requires three parameters:  Server name : name or URL of the MySQL server  Username : The username in MySql.  Password : password identified by username
    6. 6. 6 Steps for Accessing MySQL Database 1.Open a connection to MySQL 2.Select a particular database $connection = mysql_connect(host,username, password); $db = mysql_select_db( database_name , $connection ); $connection = mysql_connect("", "stud0011", "iiiiiiii"); $db = mysql_select_db("test" , $connection );
    7. 7. 7 Steps for Accessing MySQL Database (cont.) 3. Access the tables in the database as required a. Create an SQL command string containing your query. $sql = "sql_command parameters"; $sql = "SELECT * FROM contact";
    8. 8. 8 Steps for Accessing MySQL Database (cont.) b. Execute the query c. Deal with the result of the query $result = mysql_query( $sql , $connection ) or die("Couldn't execute query"); while($row = mysql_fetch_array($result)){ ... } "SELECT * FROM contact";
    9. 9. 9 Creating an SQL Table A bit tedious process Easier if you could use phpMyAdmin CREATE TABLE table_name ( fieldname1 data_type(size); fieldname2 data_type(size); fieldname3 data_type(size); . . . fieldname-n data_type(size); );
    10. 10. 10 SQL Commands The main day-to-day queries used from PHP scripts:  INSERT INTO tablename ... VALUES ... ;  REPLACE INTO tablename ... VALUES ... ;  DELETE FROM tablename WHERE ... ;  UPDATE tablename SET ... WHERE ... ;  SELECT ... FROM tablename WHERE ... ORDER BY... ;
    11. 11. 11 SQL Commands (cont.) When writing SQL commands into PHP scripts the usual problem of too many quotes arises and 'internal' quotes (") have to be escaped. SQL query must finish with a semicolon, as must a PHP script command.
    12. 12. 12 Examples of Built-In MySQL Functions  Returns an array that represents all the fields for a row in the result set.  Eg. $row = mysql_fetch_row($result)  Moves the internal row pointer of a result to the specified row, with rows counting from zero.  Use this function with mysql_fetch_row to jump to a specific row.  Eg. $row = mysql_data_seek($result, 3) array mysql_fetch_row(resource result) boolean mysql_data_seek(resource result, integer row)
    13. 13. 13 Built-In MySQL Functions array mysql_fetch_array(resource result,integer type) Returns an array that represents all the fields for a row in the result set. Value is stored twice: Once indexed by offset starting from zero, once indexed by the name of the field. Eg. $row = mysql_fetch_array($result, 5) array mysql_fetch_assoc(resource result) Returns an array that represents all the fields for a row in the result set. Element is indexed by the names of the field only Eg. $row = mysql_fetch_assoc($result)
    14. 14. 14 Examples To create a web page that linked to sale items record from a database. Step 1: Create the table named catalog in a database CREATE TABLE catalog ( ID INT(11) NOT NULL AUTO_INCREMENT, Name CHAR(32), Price DECIMAL(6,2), PRIMARY KEY (ID) );
    15. 15. 15 Example Step 2 : Insert some items along with some dummy prices. INSERT INTO catalog (Name, Price) VALUES (' Shampoo ' , 6.50), (' Brush ', 2.00), (' Conditioner ', 7.00), (' Toothbrush ', 3.50), (' Dental Floss ', 5.00); SELECT * FROM catalog
    16. 16. 16 Example Step 3: Write a PHP script to retrieve the data from table and display them in HTML table <?php //connect to server, then test for failure if(!($dbLink = mysql_connect(" localhost ", " user1", ""))) { print("Failed to connect to database!<br>n"); print("Aborting!<br>n"); exit(); } //select database then test for failure if (!($dbResult = mysql_query("USE test", $dbLink))) { print("Cannot use the test database!<br>n"); print("Aborting!<br>n"); exit(); } Database connection No password query
    17. 17. 17 Example Creating HTML table from a query (cont.) //get eveything from catalog table $Query = "SELECT Name, Price " . "FROM catalog " . "ORDER BY Name "; if(!($dbResult = mysql_query($Query, $dbLink))) { print("Could not execute query!<br>n"); print("MySQL reports: " . mysql_error() . !<br>n"); print("Query was: $Query<br>n"); exit(); }
    18. 18. TAB1033 - Internet Programming 18 Example Creating HTML table from a query (cont.) //start table print("<table border = "0 ">n"); //create header row print("<tr>n"); print("<td><b>Item</b></td>n"); print("<td><b>Price</b></td>n"); print("</tr>n"); //get each row while ($dbRow = mysql_fetch_assoc($dbResult)) { print("<tr>n"); print("<td>{$dbRow['Name']}</td>n"); print("<td align=right">{$dbRow['Price']}</td>n"); print("</tr>n"); } //end table print("</table>n"); ?> Fetch each row from the result set. Expresses each column in the result as an element of an associative array. Field name
    19. 19. 19 Output
    20. 20. 20 Drop Table DROP TABLE IF EXIST catalog;
    21. 21. 21 Changing Data With the Update Statement UPDATE catalog SET price = 10.00 WHERE name = 'Shampoo'; UPDATE table_name SET fieldname1 = value WHERE fieldname2 = value;
    22. 22. 22 Delete Data? DELETE FROM catalog WHERE name = 'Shampoo' AND price < 7.00 ; DELETE FROM table_name WHERE fieldname2 = value;
    23. 23. 23 Summary Database Connection  For example:  Host:  Username:stud9988  Password: mypass123  Database Name : db9988  Connection $connection = mysql_connect("", "stud9988", "mypass123");
    24. 24. 24 Summary Select database $db = mysql_select_db("db9988" , $connection ); Select data from table student in database db9988 $query = "SELECT id, name FROM student WHERE id = ‘1234'"; $result = mysql_query($query,$connection) or die("Couldn't execute query"); The result (output) while($row = mysql_fetch_row($result)){ print($row[0]);//1234 print($row[1]);//Ali } id name 1234 Ali 6789 Mary student
    25. 25. 25 Summary Update data $query1 = "UPDATE student SET name = 'Ahmad' WHERE id = 1234"; • Delete data $query2 = "DELETE FROM student WHERE name = 'Mary'"; Drop table $query3 = "DROP TABLE IF EXIST student;"; id name 1234 Ali 6789 Mary student
    1. A particular slide catching your eye?

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