Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP and SQL Server: Connection IST 210: Organization of Data IST2101.

Similar presentations


Presentation on theme: "PHP and SQL Server: Connection IST 210: Organization of Data IST2101."— Presentation transcript:

1 PHP and SQL Server: Connection IST 210: Organization of Data IST2101

2 Typical web application interaction (php, jsp…) 2IST210 PHP includes functionality that allows you to work directly with different types of databases

3 When you query a database… IST2103 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 4IST210

5 Example http://my.up.ist.psu.edu/zul17/query.php IST2105

6 HTML Form and PHP IST2106 Get the table name from HTML form In PHP, fetch the table name from $_POST

7 Step 1. Open a DB Connection IST2107 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 IST2108 sqlsrv_query() http://php.net/manual/en/function.sqlsrv-query.phphttp://php.net/manual/en/function.sqlsrv-query.php

9 IST2109 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 Step 3. Display Results on Webpage IST21010 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

11 IST21011 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

12 Step 4. Close a DB connection You should close a DB connection to release resources 12IST210

13 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 IST21013

14 Step 1 IST21014 Webspace Make sure the filename is query.php And the type is “All Files”

15 Step 2 IST21015 Input your own information Forgot your password? Go to https://www.up.ist.psu.edu/db/mssql.phphttps://www.up.ist.psu.edu/db/mssql.php

16 Step 3 IST21016 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


Download ppt "PHP and SQL Server: Connection IST 210: Organization of Data IST2101."

Similar presentations


Ads by Google