Client: UI HTML, JavaScript, CSS, XML Use of PHP for DB Connection Middle Get all books with keyword web programming PHP Format the output, i.e., data returned from the DB SQL DB Query Access/MySQL 1 2 Middle and Information Tier PHP: built in library functions for interfacing with the mysql database management system $id = mysqli_connect(string hostname, string username, string password, string db) Connection to the MySQL server Return false if fails $result = $id->query(string query) Execute a SQL query: return a result handle that will be used to fetch the result 3 4
Steps Middle and Information Tier PHP: built in function for mysql $result->data_seek(0); Go to the first row of the result $row=$result->fetch_assoc() Fetch the row of the results Combine with a while loop to read all rows $id->close() Close the connection Create DB pharmacy dbpharmacy.txt localhost/phpmyadmin 5
DB connection Define a SQL string Execution Data processing 11 12
Exercise Modify previous examples to show the following info: Database Modification: Insert Syntax: INSERT INTO table_name VALUES (value1, value2,.) Step: DB connection Define a string containing the SQL command Execute the sql statement 13 14 Database Modification: Modify Syntax: UPDATE table_name SET column_name_i = new_value where column_name_j=some_value Step: DB connection Define a string containing the SQL command Execute the sql statement AJAX and DB 15 16
AJAX and Database Display info in one page Display info whenever the info is received A Possible Approach Using get method If there is a salesperson s name in the URL, then display the required information Otherwise, display the form only Require HTML or PHP file? Two parts: a form to be displayed A db query to display the required info 17 18 <form method="get" action= query2.php"> <p> Select a salesperson to display the info: <select name="select"> <option value="abel" selected="selected"> Abel </option> <option value="baker"> Baker </option> <option value="jones"> Jones </option> <option value="kobad"> Kobad </option> <option value="murphy"> Murphy </option> <option value="zenith"> Zenith </option> </select> </p> <p> <input type="submit" value="query" /> </p> </form> 20
<?php if (isset($_get["select"]) and $_GET["select"]!=NULL) { $q = $_GET["select"]; $conn = mysqli_connect("localhost", "root", "","pharmacy"); if ($conn->connect_error) { echo "Unable to connect to database"; exit; print("<h1> Sales person: ". $q. "</h1>"); $query1 = "select salary from salesperson where SalesName= '". $q. "'"; $result1 = $conn->query($query1); if (!$result1) die("no information"); $result1->data_seek(0); while ($row=$result1->fetch_assoc()) { print("<h2> Salary: ". $row["salary"]. "</h2>"); 21 Browser Request the php file Display the form only A get request Display the form + order info Asynchronous requests Server Asynchronous Synchronous 22 AJAX Approach 2 files Html file A form A JavaScript function Make asynchronous connection Call to the php file, display the information when it is ready Php file Query the db to get the desired info <script type="text/javascript"> var xmlhttp; function showinfo(str) { try{ xmlhttp = new XMLHttpRequest(); catch (e){ try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); catch (e) { try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); catch (e){ alert( Error!"); return ; 23 24
var url = "phpsales.php?q=" + str; xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate == 4) { disinfo.innerhtml = xmlhttp.responsetext; xmlhttp.open("get", url, true); xmlhttp.send(null); </script> <form> <p> Select a salesperson to display the info: <select name="select" onchange="showinfo(this.value)"> <option value="abel" selected="selected"> Abel </option> <option value="baker"> Baker </option> <option value="jones"> Jones </option> <option value="kobad"> Kobad </option> <option value="murphy"> Murphy </option> <option value="zenith"> Zenith </option> </select> </p> </form> <div id="disinfo"> </div> 25 26 display.php $q = $_GET["select"]; $conn = mysqli_connect("localhost", "root", "","pharmacy"); if ($conn->connect_error) { echo "Unable to connect to database"; exit; print("<h1> Sales person: ". $q. "</h1>"); $query1 = "select salary from salesperson where SalesName= '". $q. "'"; $result1 = $conn->query($query1); if (!$result1) die("no information"); $result1->data_seek(0); while ($row=$result1->fetch_assoc()) { print("<h2> Salary: ". $row["salary"]. "</h2>"); 27
POST var url = display.php"; var queryvalue = encodeuricomponent(str); var parameters = "q="+queryvalue; xmlhttp.open("post", url, true); xmlhttp.setrequestheader("content-type", "application/x-www-form-urlencoded"); xmlhttp.send(parameters); Application design Design application in tiers Each tier can be run a separate machine, allowing for improved processing performance Promote scalability Ease long term maintenance requirements for your code Client tier Middle tier Information tier 29 30 Application design PHP: Server side scripting language SQL: Language used to query the relational database WHERE, ORDER BY, COUNT(*) Processing in PHP or SQL? Sorting, pattern matching in records Application design SQL: Language specifically designed to query and retrieve data from your tables!! Filter out unnecessary data using SQL, leaving a relevant data set for PHP to work with Selecting statement, WHERE condition ORDER BY: sorting FASTER CODE, NETWORK LOAD 31 32
Setting up a site Components Design the web documents Client side techniques: CSS, JavaScript, DHTML Server side techniques Scripting languages, how to access database Administrating a web site: IMPORTANT Difficult to keep all documents/information up to date easily Information Maintenance of a content-driven site can be a real pain Solution: database-driven site design Complete separation between the site design and the content you want to present Don t write an HTML file for every page of your site Write a page for each kind of information you want to present 33 34 Idea Model Database: content of the site Dynamically pulled from the database to create web pages Template HTML page: For each kind (type) of information browser Web server PHP file MySQ L On the visitor s end: Expects to view a standard HTML web page On the other end Have a database that only understands how to respond to SQL queries PHP scripting language go-between that speaks both languages 35 36