Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:

Similar presentations


Presentation on theme: "CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:"— Presentation transcript:

1 CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Email Address: meyer@sci.brooklyn.cuny.edu Course Page: http://www.sci.brooklyn.cuny.edu/~meyer/ http://www.sci.brooklyn.cuny.edu/~meyer/ CISC3140-Meyer-lec6 1

2 Content Connecting to a MySQL Database Disconnecting from a MySQL Database Error Checking Executing SQL Statements Useful Functions Sample Code CISC3140-Meyer-lec6 2

3 Using MySQL and PHP There are five things you want to be able to do from within PHP in relation to a MySQL database 1.Connect to the MySQL database 2.Disconnect from the MySQL database 3.Execute MySQL queries 4.Work with the results from an SQL query 5.Check the status of your MySQL commands Queries can be any kind of MySQL query, including SELECT,UPDATE,INSERT, etc. Following SELECT queries, you can execute MySQL / PHP functions to put the data read from the MySQL database into your PHP variables CISC3140-Meyer-lec6 3

4 Connecting to a MySQL database Connecting to the database: $conn = mysql_connect( $mysql_host, $mysql_user, $mysql_password ) or die('Could not connect:'.mysql_error() ); print "Connected successfully"; mysql_select_db($mysql_db) or die('Could not select database’); Replace the variables $mysql_host, $mysql_user, $mysql_password and $mysql_db with strings containing the values for your database Notice that there are two functions invoked One that logs into MySQL: mysql_connect() One that selects the database to use: mysql_select_db() CISC3140-Meyer-lec6 4

5 On " or die() " According to the PHP manual die() is a "language construct equivalent to exit()."exit() die() can be called independently and used to dump error information to the screen or to a file. "or die()" uses short circuit testing to call die IF the statement preceding die returns false. Increasingly using "or die()" in production code is seen as bad form and/or a security risk… thususing "or die()" in production code is seen as bad form and/or a security risk CISC3140-Meyer-lec6 5

6 PHP Exceptions PHP also support error handling in ways other than die() including exceptions: if ( !$result = mysql_query('SELECT foo FROM bar', $db) ) { throw new Exception('You fail: '. mysql_error($db)); } Placed within a try/catch block the code above could recover "gracefully" from a db error. Feel free to use "or die()" in this class. See also: trigger_error() CISC3140-Meyer-lec6 6

7 Disconnecting From the MySQL Database Disconnect from MySQL database: mysql_close( $conn ); CISC3140-Meyer-lec6 7

8 Check the Status of your MySQL Commands If errors occur, the functions return errors These errors can be read as strings using: mysql_error() Note the usage in this statement: $conn = mysql_connect( $mysql_host, $mysql_user, $mysql_password) or die('Could not connect:’.mysql_error()); echo 'Connected successfully‘; CISC3140-Meyer-lec6 8

9 Execute MySQL Queries (SELECT) // set up and execute the MySQL query $query = 'SELECT * FROM my_table'; $result = mysql_query($query) or die('Query failed: ‘.mysql_error()); // print the results as an HTML table print " \n"; while ($row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) { echo "\t \n"; foreach ( $row as $item ) { echo "\t\t $item \n"; } echo "\t \n"; } echo " \n"; // free result mysql_free_result( $result ); CISC3140-Meyer-lec6 9

10 Database Functions Execute the query and store the result in a local variable: mysql_query() Parse the data read returned from the query as an array: mysql_fetch_array() Free the memory used by the query result: mysql_free_result() NOTE that if the result returned is a scalar and not an array, then only mysql_query() needs to be called and does not need to be followed by a call to mysql_fetch_array(). Finally, note the use of mysql_error() in the query function. CISC3140-Meyer-lec6 10

11 Other Handy Functions Here are a few of the more handy functions: int mysql_num_rows ( $result ) Returns the number of rows contained in a $result; this is relevant when the result returned from mysql_query() is an array and not a scalar int mysql_affected_rows ($conn) This function returns the number of rows that were affected by the most recently executed INSERT, UPDATE, REPLACE or DELETE query (i.e. if the number of rows you expected to be updated were actually updated) CISC3140-Meyer-lec6 11

12 Even more handy functions int mysql_insert_id ( $conn ) this function is used when inserting a row into a table that has an AUTO_INCREMENT ID field; the function returns the ID number that was generated string mysql_stat ( $conn ) This function returns a string containing information about the status of the current database connection CISC3140-Meyer-lec6 12

13 Sample Program <?php $conn = mysql_connect("localhost",“username",”password”) or die('Could not connect:'.mysql_error()); $db_found = mysql_select_db("pgrabowiec"); if ($db_found) { $query = "SELECT thename FROM friends"; $result = mysql_query($query) or die('Query failed: '.mysql_error()); print " \n"; while ($db_fields = mysql_fetch_assoc($result)) { print "\t ".$db_fields['thename']." “; } print " \n"; } else print "Database not found"; mysql_close($conn); ?> CISC3140-Meyer-lec6 13

14 You Can Do This!!! MySQL and PHP online documentation PHP Site ▫http://www.php.net/manual/en/ref.mysql.phphttp://www.php.net/manual/en/ref.mysql.php W3c-Schools ▫http://www.w3schools.com/php/php_mysql_intro.asphttp://www.w3schools.com/php/php_mysql_intro.asp CISC3140-Meyer-lec6 14

15 Lab 4.0 – PHP & MySQL Available online Assumes you have completed the MySQL labs. Get started, if you can't finish it in class, then finish it over the weekend. Hook the pages that you create to the index page in your public_html folder. Check out the following site for help: http://146.245.250.215/~mmeyer/classexamples.html http://146.245.250.215/~mmeyer/classexamples.html CISC3140-Meyer-lec6 15


Download ppt "CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:"

Similar presentations


Ads by Google