DM2 Week 15 PHP Mysql.Pptx
Total Page:16
File Type:pdf, Size:1020Kb
PHP and MySQL ATLS 3020 Digital Media 2 Aileen Pierce Web Database Applications PHP Working with Databases ¤ PHP scripts can easily access many different databases ¤ MySQL ¤ Oracle ¤ Informix ¤ mSQL ¤ ODBC ¤ PHP provides functions that begin with mysqli_ to access MySQL databases. PHP Communicating with MySQL ¤ PHP works with MySQL using the following process: 1. Connect to MySQL 2. Prepare a SQL statement 3. Execute the statement and save the result 4. Extract the data from the result 5. Prepare the resulting page Connecting PHP to MySQL ¤ Connect to MySQL $dbc= mysqli_connect(“hostname”, “username”, “password”, “db_name”); ¤ Hostname is the URL of the MySQL server. ¤ Use localhost if PHP and MySQL servers are on the same machine (as on redwood). ¤ Username and password are for MySQL. ¤ Database name is identikey+db (apiercedb) ¤ Must assign the connection to a variable to use throughout your script. Connecting PHP to MySQL ¤ mysqli_connect_error() returns an error if the connection is not made. $dbc= mysqli_connect(“hostname”, “username”, “password”, “db_name”) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() ); ¤ die() will cause the script to exit ¤ Prints out an error message SQL Statements ¤ The mysqli_query() function allows you to pass any SQL command to the database and the result is returned. $result= mysqli_query(“db connection”, “SQL”); ¤ Use phpmyadmin to help you create the SQL statement $result = mysqli_query($dbc, “SELECT * from drink” ); SQL Statements ¤ Or assign the SQL statement to a variable $sql = “INSERT INTO drink (name, caf, whip, calories) VALUES ('cappuccino', 'yes', 'no', '90')”; ¤ Then pass the SQL statement to the database connection $result = mysqli_query($dbc, $sql); ¤ You must assign the result to a variable. SQL Statements ¤ Use phpmyadmin to help you create the SQL statement ¤ The INSERT, UPDATE and DELETE statements return TRUE if the statement ran without an error, FALSE if it wasn’t. ¤ Use phpmyadmin to check that the data was updated as you intended. Closing the Connection ¤ The mysqli_close() function closes the connection to the database server. ¤ Your script will still run if you do not include this command but too many open MySQL connections can cause problems for a web host. ¤ It is good practice to always include this line once you have issued all your commands to the database, to keep the server running well. Lab ¤ Build upon the phonelist table and data you created in MySQL. ¤ Write a PHP script that inserts data into your phonelist table. Query Results ¤ For statements such as SELECT you need to handle the results. $result = mysqli_query($dbc, “SELECT * from drink”); ¤ You must assign the result to a variable. ¤ For queries the result is a pointer to the resulting data. Query Results ¤ mysqli_fetch_array() returns one row of data at a time in an array format ¤ A loop can be used to access every returned row as long as there are more rows to be read. while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { print results } ¤ mysqli_assoc returns the array as an associative array Query Results ¤ mysqli_free_result($result) frees up the query result resources once you’re done using them. Counting Query Results ¤ mysqli_num_rows($result) returns the number of rows retrieved in the result of the query. Security ¤ Using the same username and password you use to log into MySQL will give your program the same access you have. ¤ In production systems you would create a special account which is more restricted. ¤ Username and password should also be obscured from your published code. ¤ Use an include file and put it outside the web directory Debugging MySQL ¤ Most common MySQL errors ¤ Unbalanced quotation marks or parentheses ¤ Misspelled column or table name ¤ Unescaped apostrophes in column values ¤ Wrong order of query clauses ¤ Debugging steps ¤ Print out your query in PHP (print $sql) ¤ Run the query directly in phpmyadmin ¤ Strip the query to it’s most basic form then add back in clauses to figure out the problem Lab ¤ Write a PHP script that queries data in your phonelist table and displays it in a web page. .