PHP AND SQL SERVER: QUERIES IST 210: Organization of Data IST210 1
Three-Tier Architecture Three-tier architecture means that the Web server and the DBMS are on separate servers IST210 2 Client (PC) Web Server DBMS PHP is run on the web server Browser Microsoft SQL Management Studio Our technical setting
What we have learned PHP and SQL connection IST210 3
4 PHP SQL Example Query Which table to query: What attributes to query (separated by,):
IST210 5 <?php $server = "mssql.up.ist.psu.edu"; $sqlUsername = "Your-SQL-Username"; $sqlPassword = "Your-SQL-Password"; $databaseName = "Your-PSU-ID"; $connectionInfo = array('Database'=>$databaseName, 'UID'=>$sqlUsername, 'PWD'=>$sqlPassword, 'Encrypt'=>'0', 'ReturnDatesAsStrings'=>true ); $connection = sqlsrv_connect( $server, $connectionInfo ) or die("ERROR: database parameters are not correct");
IST210 6 if (!empty($_POST['table'])) { // get the table name $table = $_POST['table']; if (!empty($_POST['attr'])) { $attr = $_POST['attr']; $query = "SELECT $attr FROM $table"; } else { // prepare SQL query $query = "SELECT * FROM $table"; } // print the query echo "Query: ".$query." ";
IST210 7 // Execute SQL query $result = sqlsrv_query($connection, $query) or die( "ERROR: Query is wrong"); echo " "; // fetch attribute names foreach( sqlsrv_field_metadata($result) as $fieldMetadata) echo " ".$fieldMetadata['Name']." "; echo " "; // fetch rows in the table while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) ) { echo " \n"; foreach ($row as $cell) { echo " $cell "; } echo " \n"; } echo " ";
Today’s Objectives Answer different queries from PHP Cases: (challenging) exercise (challenging) exercise IST210 8
Prepare Database We will use the database in Chapter 3 If you delete the database in Chapter 3, download the SQL script and create the database again (see the last few slides) IST210 9
Case 1. Submit – Manual Coding IST210 10
Case 1. Code Specification IST case1.php
Case 1. Code Specification (cont.) IST case1.php process_case1.php
Problem with Manual Coding Cannot dynamically update information What if we delete some departments in the database? We need to change PHP page! Bad! Database and webpages are separate layers Easy to make bugs “administration” is misspelled Solution? Dynamically generate the departments by querying the data from the database! IST210 13
Case 1a. Submit – Automatic Coding IST210 14
Case 1a. Code Specification IST case1a.php Get the department names from database Dynamicall y generate the submit buttons
Compare Case 1 and Case 1a IST Case 1: manually code all the submit buttons Case 2a: automatically generate all the submit buttons Important lines!
Case 1a. Code Specification IST process_case1a.php Same as process_case1.php except “Go back” action
Case 2. Dropdown List – Manual Coding IST210 18
Case 2. Code Specification IST case2.php
Case 2. Code Specification (Cont.) IST Process_case2.php
Case 2a. Dropdown List – Automatic Coding IST210 21
Case 2a. Code Specification IST case2a.php
Compare case2.php and case2a.php IST Case2.php Manual coding Case2a.php Automatic coding
In-Class Exercise Get the cases run on your webspace IST210 24
Step 1 Download the cases.zip from wikipage Unzip it, you will 8 PHP files case1.php, process_case1.php case1a.php, process_case1a.php case2.php, process_case2.php case2a.php, process_case2a.php Copy all the files to your webspace IST210 25
Step 2 Open php files using NotePad++ and modify the data information IST Input your own information
Step 3 Visit your php page and query your database IST210 27
Prepare Database If you don’t have the tables and data in your database… IST210 28
Prepare Database We will use the database in Chapter 3 IST210 29
Prepare Database (cont.) Log on an IST Windows machine If not in the lab, use remote desktop Run the SQL Server application Start Application Development and Management Microsoft SQL Server 2014 SQL Server 2014 Management Studio Parameters Server Type: Database Engine Server Name: upsql Authentication: Windows Authentication Alternate Authentication (connect to the same database): SQL Server Authentication Get your SQL account: Hit “Connect” IST210 30
Prepare Database (cont.) Download scripts on our website Open them in SQL management studio and run them. 1. Delete all the tables 2. Create new database 3. Insert data IST210 31
Prepare Database (cont.) You should now have 4 tables in your database If not, right click Tables, and click “refresh” Check whether you already have the data in your tables Right click on a table and click “Select Top 1000 Rows” IST210 32