1 / 10

Introduction

Introduction. We are going to look at some working code It writes fixed data into a simple one-table database We will look at the key parts of the code in detail. Database Structure. Test database has one table “tblWeek6”

bien
Download Presentation

Introduction

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. Introduction • We are going to look at some working code • It writes fixed data into a simple one-table database • We will look at the key parts of the code in detail

  2. Database Structure • Test database has one table • “tblWeek6” • What would happen if we named a table “select” or “update” or “values” or “table of users”? • The table has just four columns • id, an “auto-increment” integer field • twLastname, text, a varchar(30) field • twFirstname, text, also a varchar(30) field • twPassword, text, as above

  3. Standard HTML header Example Code • <html> • <head> • <title>Writing Simple Data</title></head> • <body> • <h2>Writing Simple Data</h2> • <?php • //=====================================// • // The data we are going to store • // Fixed data for this simple test. • $sLastname = "Sanchez"; • $sFirstname = "Luis"; • $sPassword = "lsanchez"; • $nID = 65; Switches into PHP mode Provides some fixed data to work with – Normally this would come from a Form

  4. The [action] or die(“message”) block allows us to view simple errors Example Code • // Connects to a MySQL server • $myID='xy123456'; • $mysqli = new mysqli("web.fcet.staffs.ac.uk",$myID,$myID,$myID) or die("Failed to connect to DB" . $mysqli->error); • //=====================================// • // Inserts new data into our users table • try • { $bItWorked = $mysqli->query( • "INSERT INTO tblWeek6 • (id, twLastname, twFirstname, twPassword) • VALUES • ($nID, '$sLastname', '$sFirstname', '$sPassword')" ); try/catch block allows us to catch errors

  5. Example Code • if($bItWorked) • { echo "Successfully added the data ($nID, '$sLastname', '$sFirstname', '$sPassword') to the database.<br />"; • } • else • { echo "Failed to add the data ($nID, '$sLastname', '$sFirstname', '$sPassword') to the database!<br />"; • } • } • catch(Exception $e) • { echo 'Sorry - There was a problem with adding the data to the database.<br />'; • }

  6. Example Code Frees up resources – just good manners here, can be important in bigger scripts [why?] • // closes the connection, frees up resources • $mysqli->close(); • $mysqli = null; • ?> • </body> • </html>

  7. Key parts $mysqli = new mysqli("web.fcet.staffs.ac.uk", $myID, $myID, $myID); • This creates a new connection to our mySQL database • www.fcet.staffs.ac.uk is the database server • $myIDis the username, password and db name

  8. Key parts $bItWorked = $mysqli->query( "INSERT INTO tblWeek6 (id, twLastname, twFirstname, twPassword) VALUES ($nID, '$sLastname', '$sFirstname', '$sPassword')" ); • Tries to run the SQL in the database (returns FALSE on failure, TRUE on success) • May cause an error, which we can trap • Notice the single quotes around string parameters • Notice the double quotes allowing “variable interpolation”

  9. Re-use • The highlighted code is worth re-using • Makes sense to copy and paste this “boilerplate” code and modify it for your needs • Usually only needs the username to be changed • try/catch blocks are useful in lots of areas, not just database handling

  10. Summary • We have seen some very simple PHP code • Parts of the application presented can be used as “boilerplate code” • Database access in PHP is quite simple to do • There are pitfalls for the unwary

More Related