Download presentation
Presentation is loading. Please wait.
Published byFlorence White Modified over 8 years ago
1
PHP AND SQL SERVER: QUERIES IST 210: Organization of Data IST210 1
2
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
3
What we have learned PHP and SQL connection http://my.up.ist.psu.edu/duw24/query-attr.php http://my.up.ist.psu.edu/duw24/query-attr.php IST210 3
4
4 PHP SQL Example Query Which table to query: What attributes to query (separated by,):
5
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");
6
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." ";
7
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 " ";
8
Today’s Objectives Answer different queries from PHP Cases: http://my.up.ist.psu.edu/duw24/case1.php http://my.up.ist.psu.edu/duw24/case1a.php http://my.up.ist.psu.edu/duw24/case2.php http://my.up.ist.psu.edu/duw24/case2a.php http://my.up.ist.psu.edu/duw24/case3.php (challenging) exercise http://my.up.ist.psu.edu/duw24/case4.php (challenging) exercise IST210 8
9
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
10
Case 1. Submit – Manual Coding http://my.up.ist.psu.edu/duw24/case1.php IST210 10
11
Case 1. Code Specification IST210 11 case1.php
12
Case 1. Code Specification (cont.) IST210 12 case1.php process_case1.php
13
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
14
Case 1a. Submit – Automatic Coding http://my.up.ist.psu.edu/zul17/case1a.php IST210 14
15
Case 1a. Code Specification IST210 15 case1a.php Get the department names from database Dynamicall y generate the submit buttons
16
Compare Case 1 and Case 1a IST210 16 Case 1: manually code all the submit buttons Case 2a: automatically generate all the submit buttons Important lines!
17
Case 1a. Code Specification IST210 17 process_case1a.php Same as process_case1.php except “Go back” action
18
Case 2. Dropdown List – Manual Coding http://my.up.ist.psu.edu/duw24/case2.php IST210 18
19
Case 2. Code Specification IST210 19 case2.php
20
Case 2. Code Specification (Cont.) IST210 20 Process_case2.php
21
Case 2a. Dropdown List – Automatic Coding http://my.up.ist.psu.edu/duw24/case2a.php IST210 21
22
Case 2a. Code Specification IST210 22 case2a.php
23
Compare case2.php and case2a.php IST210 23 Case2.php Manual coding Case2a.php Automatic coding
24
In-Class Exercise Get the cases run on your webspace IST210 24
25
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
26
Step 2 Open php files using NotePad++ and modify the data information IST210 26 Input your own information
27
Step 3 Visit your php page and query your database http://my.up.ist.psu.edu/YourPSUID/case1.php http://my.up.ist.psu.edu/YourPSUID/case1.php http://my.up.ist.psu.edu/YourPSUID/case1a.php http://my.up.ist.psu.edu/YourPSUID/case1a.php http://my.up.ist.psu.edu/YourPSUID/case2.php http://my.up.ist.psu.edu/YourPSUID/case2.php http://my.up.ist.psu.edu/YourPSUID/case2a.php http://my.up.ist.psu.edu/YourPSUID/case2a.php IST210 27
28
Prepare Database If you don’t have the tables and data in your database… IST210 28
29
Prepare Database We will use the database in Chapter 3 IST210 29
30
Prepare Database (cont.) Log on an IST Windows machine If not in the lab, use remote desktop https://www.up.ist.psu.edu/vlabs/ 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: https://www.up.ist.psu.edu/db/mssql.php https://www.up.ist.psu.edu/db/mssql.php Hit “Connect” IST210 30
31
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
32
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.