Download presentation
Presentation is loading. Please wait.
Published byCuthbert Eaton Modified over 8 years ago
1
PHP AND SQL SERVER: CONNECTION IST 210: Organization of Data IST210 1
2
Typical web application interaction IST210 2 (php, jsp…) PHP includes functionality that allows you to work directly with different types of databases
3
When you query a database… IST210 3 Step2. Select the database Step1. Connect to the MS SQL server Server Name: updb1 Authentication: SQL Server Authentication Step3. Input a query and execute it Step4. When you are done, close the application
4
Let PHP do that … Basic Steps for PHP Database Access 1. Connect to the MS SQL server 2. Perform SQL operations 3. Display results on webpages 1. Get the results and manipulate on the website 4. Disconnect from the server IST210 4
5
Example https://my.up.ist.psu.edu/duw24/query.php IST210 5
6
HTML Form and PHP IST210 6 Get the table name from HTML form In PHP, fetch the table name from $_POST
7
Step 1. Open a DB Connection IST210 7 Forgot your password? Go to https://www.up.ist.psu.edu/db/mssql.phphttps://www.up.ist.psu.edu/db/mssql.php sqlsrv_connect(): http://php.net/manual/en/function.sqlsrv-connect.phphttp://php.net/manual/en/function.sqlsrv-connect.php
8
Step 2. Use SQL to Query your DB IST210 8 sqlsrv_query() http://php.net/manual/en/function.sqlsrv-query.phphttp://php.net/manual/en/function.sqlsrv-query.php
9
IST210 9 If input table is “project”, the generated query is “SELECT * FROM project” Execute the SQL command “SELECT * FROM project” Same as you execute SQL in Management studio Tip: If your code has bug, it is good to print your query on webpage, and try to run it in the management studio. It could be the way you generate the query is wrong.
10
IST210 10 If input table is “project”, the generated query is “SELECT * FROM project” Execute the SQL command “SELECT * FROM project” If the above is your only code. This is what you will see when typing in department
11
Step 3. Display Results on Webpage IST210 11 sqlsrv_fetch_array(): http://php.net/manual/en/function.sqlsrv-fetch-array.phphttp://php.net/manual/en/function.sqlsrv-fetch-array.php sqlsrv_field_metadata(): http://php.net/manual/en/function.sqlsrv-field-metadata.phphttp://php.net/manual/en/function.sqlsrv-field-metadata.php
12
IST210 12 Result in management studio Result shown on webpage (in table) Put the result in the table Fetching all the fields and print the field names The header is the field Fetching each row of the result Fetching each column of that row $row is an array, $cell is an element in the array Refer to previous lectures about array, loop, and table
13
IST210 13 Put the result in the table Fetching all the fields and print the field names Fetching each row of the result Fetching each column of that row $row is an array, $cell is an element in the array Refer to previous lectures about array, loop, and table
14
Step 4. Close a DB connection You should close a DB connection to release resources IST210 14
15
In-Class Exercise 1. Download the query.php to your webspace on our course webpage 2. Modify the database information 3. Connect web server and database server IST210 15
16
Step 1 IST210 16 Webspace Make sure the filename is query.php And the type is “All Files”
17
Step 2 IST210 17 Input your own information Forgot your password? Go to https://www.up.ist.psu.edu/db/m ssql.php https://www.up.ist.psu.edu/db/m ssql.php
18
Step 3 IST210 18 Note: you may not have PROJECT table in your database! Check out what tables you have! Go to MS SQL Management Studio Go to your database (your PSUID) Go to Tables If you do not have any table in your database, download the scripts in SQL-1 class to create tables Visit http://my.up.ist.psu.edu/YourPSUID/query.phphttp://my.up.ist.psu.edu/YourPSUID/query.php
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.