1 / 18

Lecture 11 PHP: Database Connections

Lecture 11 PHP: Database Connections. MIS 3501, 2014 Spring Jeremy Shafer Department of MIS Fox School of Business Temple University April 3, 2014. Agenda for today. Review homework assignment The mysqli functions Authentication and Sessions Some new features in class10.zip.

kaida
Download Presentation

Lecture 11 PHP: Database Connections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 11PHP: Database Connections MIS 3501, 2014 Spring Jeremy Shafer Department of MIS Fox School of Business Temple University April 3, 2014

  2. Agenda for today • Review homework assignment • The mysqli functions • Authentication and Sessions • Some new features in class10.zip

  3. The mysqli_ functions • mysqli_connect • mysqli_select_db • mysqli_query • mysqli_fetch_array • mysqli_close See: http://www.w3schools.com/php/php_ref_mysqli.asp PHP Programming with MySQL, 2nd Edition

  4. PHP and MySQL Apache Web Service ? MySQL Databases PHP Interpreter PHP Programming with MySQL, 2nd Edition

  5. PHP and MySQL Apache Web Service MySQL Databases PHP Interpreter • mysqli_connect() • mysqli_select_db() • mysqli_query() • mysqli_fetch_array() • mysqli_close() PHP Programming with MySQL, 2nd Edition

  6. Opening a MySQL Connection • Open a connection to a MySQL database server with the mysqli_connect() function • The mysqli_connect() function returns a database resource variable (a positive integer) if it connects to the database successfully or FALSE if it does not • Assign the return value from the mysqli_connect() function to a variable that you can use to access the database in your script PHP Programming with MySQL, 2nd Edition

  7. Opening a MySQL Connection • The syntax for the mysqli_connect()function is: $connection = mysqli_connect("host", "user", "password", "database"); • The host argument specifies the host name where your MySQL database server is installed • The user and password arguments specify a MySQL account name and password • The database is the name of the database to be used PHP Programming with MySQL, 2nd Edition

  8. Opening a MySQL Connection • The database connection is assigned to the $connection variable $connection = mysqli_connect("localhost", "dongosselin", "rosebud", "dbdemo"); PHP Programming with MySQL, 2nd Edition

  9. Sending SQL statements to the database • The mysqli_query() function sends a SQL statement to the database, and gets a result back. • It has a syntax of:$Queryresult = mysqli_query($connection, “SQL STATEMENT GOES HERE”) • For INSERT, UPDATE, and DELETE statements it returns TRUE on success and FALSE on failure. • For SELECT statements it returns a resource on success and FALSE on failure. PHP Programming with MySQL, 2nd Edition

  10. What do I do with the select results? • The mysqli_fetch_array() function extracts an array out of the select results • It has a syntax of:mysqli_fetch_array( $QueryResult ) • As long as there are records in the query result, mysqli_fetch_array() will return an array. • When there are no more records in the query result, mysqli_fetch_array() will return FALSE. PHP Programming with MySQL, 2nd Edition

  11. Closing a MySQL Connection • Close a database connection using the mysqli_close() function mysqli_close($connection); PHP Programming with MySQL, 2nd Edition

  12. Authentication • Use a one-way encryption function. For example: md5() • The logic goes like this: • Never store the decrypted password • Store a one-way encrypted password instead • When a user attempts to login in, encrypt their input • Compare the two encrypted passwords to see if the match PHP Programming with MySQL, 2nd Edition

  13. Sessions • A session refers to a period of activity when a PHP script stores state information on a Web server PHP Programming with MySQL, 2nd Edition

  14. Using Sessions to Save State Information • Sessions allow you to maintain state information even when clients disable cookies in their Web browsers PHP Programming with MySQL, 2nd Edition

  15. Starting a Session • The session_start() function starts a new session or continues an existing one • The session_start() function generates a unique session ID to identify the session • A session ID is a random alphanumeric string that looks something like: 7f39d7dd020773f115d753c71290e11f • The session_start() function creates a text file on the Web server that is the same name as the session ID, preceded by sess_ PHP Programming with MySQL, 2nd Edition

  16. Starting a Session (continued) • The session_start() function does not accept any arguments, nor does it return a value that you can use in your script <?php session_start(); ... • You must call the session_start() function before you send the Web browser any output PHP Programming with MySQL, 2nd Edition

  17. Working with Session Variables • Session state information is stored in the $_SESSION autoglobal • When the session_start() function is called, PHP either initializes a new $_SESSION autoglobal or retrieves any variables for the current session (based on the session ID) into the $_SESSION autoglobal PHP Programming with MySQL, 2nd Edition

  18. Working with Session Variables (continued) <?php session_start(); $_SESSION['firstName'] = "Don"; $_SESSION['lastName'] = "Gosselin"; $_SESSION['occupation'] = "writer"; ?> <p><a href='<?php echo "Occupation.php?" . session_id() ?>'>Occupation</a></p> PHP Programming with MySQL, 2nd Edition

More Related