PHP and SQL Server: Connection IST2101. Typical web application interaction (php, jsp…) database drivers 2IST210.

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Keys, Referential Integrity and PHP One to Many on the Web.
Web Database Programming Connecting Database to Web.
HTML Form and PHP IST Review of Previous Class HTML table and PHP array Winner is chosen automatically using rand() function.
Manipulating MySQL Databases with PHP. PHP and mySQL2 Objectives Connect to MySQL from PHP Learn how to handle MySQL errors Execute SQL statements with.
Objectives Connect to MySQL from PHP
PHP and MySQL Web Development tMyn1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Website Development Working with MySQL. What you will achieve today! Connecting to mySql Creating tables in mySql Saving data on a server using mySql.
PHP Scripts HTML Forms Two-tier Software Architecture PHP Tools.
PHP and SQL Server: Queries IST2101. Three-Tier Architecture Three-tier architecture means that the Web server and the DBMS are on separate servers IST2102.
PHP and SQL Server: Queries IST2101. Project Report 4 SQL Queries Due Sunday, 4/5 at 11:59pm Instructions on how to access team webspace and SQL database.
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Deleting and Updating Records in MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
INFM 603: Information Technology and Organizational Context Jimmy Lin The iSchool University of Maryland Thursday, October 18, 2012 Session 7: PHP.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Lec_6 Manipulating MySQL Databases with PHP PHP Programming with MySQL.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
NMD202 Web Scripting Week3. What we will cover today Includes Exercises PHP Forms Exercises Server side validation Exercises.
Database Access with PHP and MySQL CS356 Examples from Web Database Applications, by Hugh E. Williams & David Lane, O'Reilly, 2002.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
HTML Form and PHP IST Review of Previous Class HTML table and PHP array Winner is chosen randomly using rand() function.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Retrieving data from MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
PHP Database Processing CIS 1715 Web Technologies.
Controlling Web Site Access Using Logins CS 320. Basic Approach HTML form a php page that collects the username and password  Sends them to second PHP.
>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
PHP and SQL Server: Queries IST2101. Steps to Design PHP Pages to Answer User Queries 1.Query generation – What SQL query do we need to retrieve the desired.
PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)
Database Access Using JDBC BCIS 3680 Enterprise Programming.
Database Access Control IST2101. Why Implementing User Authentication? Remove a lot of redundancies in duplicate inputs of database information – Your.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
>> PHP: MySQL & CRUD. R ecall Database Tables Records is composed of Operations (CRUD) Create Retrieve Update Delete DBMS Access Control MySQL phpMyAdmin.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
MySQL MySQL and PHP – interacting with a database.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
HTML FORM AND PHP IST 210: Organization of Data IST210 1.
PHP AND SQL SERVER: QUERIES IST 210: Organization of Data IST210 1.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
DATABASE ACCESS CONTROL IST Question Almost every PHP page needs to interact with database, does that mean sqlUsername and sqlPassword need to be.
PHP and SQL Server: Connection IST 210: Organization of Data IST2101.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
PHP AND SQL SERVER: CONNECTION IST 210: Organization of Data IST210 1.
ASP.NET Programming with C# and SQL Server First Edition
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Perl Database – Just Enough
ISC440: Web Programming 2 Server-side Scripting PHP 3
MySQL Web Application Connecting to a MySQL database
Lecture 2 - SQL Injection
MySQL Web Application Connecting to a MySQL database
Database Access with PHP and MySQL
Presentation transcript:

PHP and SQL Server: Connection IST2101

Typical web application interaction (php, jsp…) database drivers 2IST210

When you query a database… IST2103 Step2. Select the database Step1. Connect to the MS SQL server Server Name: upsql Authentication: SQL Server Authentication Step3. Input a query and execute it to get result Step4. When you are done, close the application

Let PHP do it for you… Basic Steps for PHP Database Access: 1.Connect to the MS SQL server and access the database – connect 2.Perform SQL operations – Query – Get the results and update on the webpage – Most of the work are in this step 3.Disconnect from the server – close 4IST210

Example IST2105

Try it yourself 1.Download query.php from the course website and save it to your IST webspace 2.Open query.php using Notepad++ and modify the database information (important!) 3.Visit the PHP page and query the database: – IST2106

Modify the Database Information IST2107 Input your own information

Connect to the Server IST2108 Attention: 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 “SQL-Create-Tables.sql” and “SQL-Insert-Data.sql” from course website (week4-2) to create tables Visit

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

Step 1. Open a DB Connection Open a connection to a database server – MS SQL Server: $connection = sqlsrv_connect( $hostName, $connectionInfo ) More spec: connect.php connect.php The sqlsrv_connect function returns a resource handle $connection if it connects to the database successfully 10IST210

11 $hostname ‘UID’ ‘PWD’ Same parameters when you try to log in SQL server using MS Management Studio: ‘Database’

Step 2-1. Use SQL to Query your DB Run a query with PHP – MS SQL Server: $query_result = sqlsrv_query($connection, $query); – It returns a result handle $query_result – We need the result handle ($query_result) to fetch result data 12IST210

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

Step 2-2. Fetch query results Fetch the fields in the results – MS SQL Server: $fieldMetadata = sqlsrv_field_metadata($query_result); $fieldname= $fieldMetadata['Name']; – More spec: Get the data from each row – MS SQL Server: $line = sqlsrv_fetch_array($query_result, SQLSRV_FETCH_ASSOC) The row ($line) is returned as an array. – More spec: 14IST210

15 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 $line is an array, $cell is an element in the array Refer to previous lectures for array, loop, and table

Step 2-other. Other queries for your DB You can also insert/update/delete records – MS SQL Example $query=“INSERT INTO USERS (UserID, UserName) VALUES(1, \ ‘ Leon\ ’ )” $query_result = sqlsrv_query($connection, $query) You can even create/delete/alter db objects – MS SQL Example $query=“DROP TABLE USERS” $query_result = sqlsrv_query($connection, $query) 16IST210

Step 3. Close a DB connection You should close a DB connection to release resources – MS SQL Server: sqlsrv_close($connection); 17IST210

Error Handling All php DB functions return NULL (or false) if they fail – The database server is not running – Insufficient privileges to access the data source – Invalid username and/or password Several functions are helpful in graceful failure – die(string) - halts and displays the string – sqlsrv_errors() - returns text of error 18IST210

Error Handling examples Method One: if (!($connection = sqlsrv_connect( $hostName, $connectionInfo ))) die("ERROR: connecting database server failed "); Method two: $connection = sqlsrv_connect( $hostName, $connectionInfo ) or die("ERROR: connecting database server failed"); 19IST210

Other PHP MSSQL Classes – Functions: allowing plugging-in variables 20IST210

In-Class Exercise Modify query.php, allowing to specify attributes – If no input for attributes, show the table – If some input for attributes, show the selected columns – Example:

Hints IST21022 You need to modify these two parts Now, you need to query given attributes not the whole table. What query you should generate? Also, when there is no input for attributes, you need to show all the results. So think about using if…else… statement You need to have a new input for attribute names