Set 5: Perl and Database Connections

Slides:



Advertisements
Similar presentations
Programming MySQL, Perl COEN 351. Reading List Paul DuBois: MySQL and Perl for the Web, New Riders, 2002 Jacqueline D. Hamilton: CGI Programming 101,
Advertisements

Social Web Design 1 Darby Chang Social Web Design.
Molecular Biomedical Informatics Web Programming 1.
Murali Mani SQL-PL Interface. Murali Mani Some Possible Options Web Interface Perl /CGI with Oracle/mySQL Install your own web server and use servlets.
Web Database Programming Connecting Database to Web.
CGI Programming.
CGI Programming Part 2. Input Tags Many different ways of getting data from the user. The tag is used most often. has a type attribute –Specifies the.
PHP (2) – Functions, Arrays, Databases, and sessions.
PHP Intro/Overview Squirrel Book pages Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a.
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Introduction to Active Server Pages
Multiple Tiers in Action
CSC3530 Software Technology Tutorial Two PERL Basics.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Defined/Undef my $i; if( defined $i ) #false $i=0; if( defined $i ) #true my %hash; #or %hash=(); defined %hash; #false, hash is empty $hash{“1”}=“one”;
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Set 5: Web Development Toolkits IT452 Advanced Web and Internet Systems.
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
1 ‘Dynamic’ Web Pages So far, we have developed ‘static’ web-pages, e.g., cv.html, repair.html and order.html. There is often a requirement to produce.
JAVASCRIPT HOW TO PROGRAM -2 DR. JOHN P. ABRAHAM UTPA.
Chapter 9 Using Perl for CGI Programming. Computation is required to support sophisticated web applications Computation can be done by the server or the.
INTERNET APPLICATION DEVELOPMENT For More visit:
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
Internet Applications Spring Review Last week –MySQL –Questions?
Lecture 14 – Web Security SFDV3011 – Advanced Web Development 1.
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
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.
Internet Forms and Database Bob Kisel Amgraf, Inc.
Information & Decision Management Dept West Texas A&M University
Perl Web Page – Just Enough Pepper. Web site Set up the top of your script to indicate perl and plain text #!/usr/bin/perl print "Content-type:text/plain\n\n";
USING PERL FOR CGI PROGRAMMING
06/10/2015AJAX 1. 2 Introduction All material from AJAX – what is it? Traditional web pages and operation Examples of AJAX use Creating.
CSU - DCE Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the.
Building PERL Scripts on a Windows system* *and running those scripts on an Apache server!
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
(Chapter 10 continued) Our examples feature MySQL as the database engine. It's open source and free. It's fully featured. And it's platform independent.
Website Development with PHP and MySQL Saving Data.
CGI Programming. What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. CGI is not a Perl-specific concept. Almost.
1 Basic Perl CGI Programming. 2 Issues How and when your program is invoked. Generating Response –HTTP Headers –HTML (or whatever document type you want)
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Databases On The Web with perl Archie Warnock
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
PHP PHP: Hypertext Preprocesor Personal Home Page Tools.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Perl CGI What is "CGI"? Common Gateway Interface A means of running an executable program via the Web. Perl have a *very* nice interface to create CGI.
PHP Open source language for server-side scripting Works well with many databases (e.g., MySQL) Files end in.php,.php3 or.phtml Runs on all major platforms.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Introduction to Programming the WWW I CMSC Winter 2003.
Java, JDBC, Perl, CGI Embedded SQL November 3 rd, 2000.
>> 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.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
PHP and SQL Server: Connection IST2101. Typical web application interaction (php, jsp…) database drivers 2IST210.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
1 AJAX. AJAX – Whatzit? Asynchronous (content loading)‏ Javascript (logic & control)‏ And XML (request handling)‏
PHP and SQL Server: Connection IST 210: Organization of Data IST2101.
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
Using the Internet to publish data and applications
Perl Database – Just Enough
PHP Intro/Overview Bird Book pages 1-11,
Tutorial 6 PHP & MySQL Li Xu
Presentation transcript:

Set 5: Perl and Database Connections IT452 Advanced Web and Internet Systems Set 5: Perl and Database Connections

(maybe??) You know how to use SQL Assumptions You know Perl We’ll review Can use PHP instead (maybe??) You know how to use SQL

Perl Basics “Scalar” variables: $x = 3; $y = "Hello"; “Array” variables: @list = (3, 7, "dog", "cat"); @list2 = @list1; # copies whole array! A single element of an array is a “scalar: print “Second item is: $list[1]”; # Don’t use @ Get array length by treating whole array as scalar: $lengthOfList2 = @list2; File operations open ( MYFILE, "input.txt" ); open ( MYFILE, “>output.txt" ); open ( MYFILE, “>>LOG.txt" ); Input Output Append

Perl Function Calls (“subroutines”) Any of the 2 calls to hello() works – no enforcement of function parameters. use CGI qw( :standard ); print header(); print start_html(); # Prints "hello", takes no arguments sub hello { print "\n<br/> Hello."; } # Takes two arguments, return their product sub multiply { my($valA, $valB) = @_; return $valA * $valB; my $x = 2; print "\n<br/> $x * 7 = " . multiply($x,7); hello(); hello(72145); print end_html();

Arrays and Parameters # Defines new global array, @array1 # AND returns a new array with 4 elements. sub makeArray { my($first, @args) = @_; @array1 = ($first, 23, 90); my @array2 = (34, 5.4, 123, 2.01); return @args; } my @test1 = makeArray(1, 2, 3, 4); print "\ntest1 is: @test1\n”; print "\narray1 is: @array1\n”; print “\narray2 is: @array2\n”; Discuss @_ usage Array1 – global Array2 - local Test1 is 2 3 4 Array1 is 1 23 90 Array2 is _________

Prepare creates an Object to handle the query Perl DB Queries Connect to the DB. my $databaseHandle = DBI->connect(“STUFF"); Create your DB query. my $query = "SELECT * FROM comments"; my $statementHandle = $databaseHandle->prepare($query); $statementHandle->execute(); Process the results. while ( my @row = $statementHandle->fetchrow_array ) { print “$row[0] \t $row[1] \t $row[2] \n”; } Disconnect. $databaseHandle->disconnect(); $statementHandle->finish(); Prepare creates an Object to handle the query

Perl DB Queries (2) my $c_id = param("comment_id"); Question mark is a parameter. Pass in value with execute(). Perl library handles injection attacks. my $c_id = param("comment_id"); my $databaseHandle = DBI->connect( “STUFF"); my $query = "SELECT * FROM comments WHERE id=?"; my $statementHandle = $databaseHandle->prepare($query); $statementHandle->execute($c_id); while (my @row = $statementHandle->fetchrow_array) { print “$row[0] \t $row[1] \t $row[2] \n”; } $databaseHandle->disconnect(); $statementHandle->finish();

Order of DB Results Array order. Hash Table order. my $query = "SELECT * FROM comments"; my $query = "SELECT id, username, comment FROM comments"; Array order. while (my @row = $statementHandle->fetchrow_array) { print “$row[0] \t $row[1] \t $row[2] \n”; } Hash Table order. while (my $hashref = $statementHandle->fetchrow_hashref){ my %hash = %{$hashref}; print “$hash{‘id’} \t $hash{‘user’} \t $hash{‘comment’} \n”; Order of the table’s creation Your order! Hash table, ignore order

Output to an HTML Page Show ex1_html_query.pl Then ex2_html_query.pl #!/usr/bin/perl use strict; use CGI::Carp qw( fatalsToBrowser ); use CGI qw( :standard ); use DBI; use DBD::mysql; print header(); print start_html(); my $c_id = param("comment_id"); my $databaseHandle = DBI->connect( “STUFF"); my $query = "SELECT * FROM comments WHERE id=?"; my $statementHandle = $databaseHandle->prepare($query); $statementHandle->execute($c_id); # put results in a table print "<table border='1'> <thead>"; print "<th> ID </th><th> User </th><th> Comment </th></thead><tbody>"; while (my @row = $statementHandle->fetchrow_array) { print “<tr><td> $row[0] </td><td> $row[1] </td><td> $row[2] </td></tr>”; } print "</tbody> </table> <br/> <hr/>"; $databaseHandle->disconnect(); $statementHandle->finish(); print end_html();

Example – Output MATCHING to TEXT Show ex3_plain.pl #!/usr/bin/perl use strict; use CGI::Carp qw( fatalsToBrowser ); use CGI qw( :standard ); use DBI; use DBD::mysql; print( "Content-type: text/plain; charset=UTF-8\n\n"); my $search = param(“search"); my $databaseHandle = DBI->connect( “STUFF"); my $query = "SELECT * FROM comments WHERE user LIKE '%$search%'"; my $statementHandle = $databaseHandle->prepare($query); $statementHandle->execute; # Output plain results. Separate lines with vertical bar while (my @row = $statementHandle->fetchrow_array) { print "$row[0],$row[1],$row[2]|\n"; } $databaseHandle->disconnect(); $statementHandle->finish(); “search” query % wildcard No footer

Example – Get from DB, output ALL to XML Show ex4_xml.pl #!/usr/bin/perl use strict; use CGI::Carp qw( fatalsToBrowser ); use CGI qw( :standard ); use DBI; use DBD::mysql; print( "Content-type: text/xml; charset=UTF-8\n\n"); my $databaseHandle = DBI->connect( “STUFF"); my $query = "SELECT * FROM comments"; my $statementHandle = $databaseHandle->prepare($query); $statementHandle->execute; # Output plain results. Separate lines with vertical bar print "<results>\n"; while (my @row = $statementHandle->fetchrow_array) { print " <result>\n"; print " <id>$row[0]</id>\n"; print " <user>$row[1]</user>\n"; print " <comment>$row[2]</comment>\n"; print " </result>\n"; } print "</results>\n"; $databaseHandle->disconnect(); $statementHandle->finish(); XML header

Headers Matter XHTML Plain Text XML print header(); print start_html(); … print end_html(); Plain Text print("Content-type: text/plain; charset=UTF-8\n\n"); XML print("Content-type: text/xml; charset=UTF-8\n\n");

Example – Simple INSERT Show ex5_insert.pl my $user = param("user"); my $comment = param("comment"); print header(); print start_html(); my $databaseHandle = DBI->connect( “STUFF"); # Do the SQL insert my $query = "INSERT INTO comments (user, blurb) VALUES (?,?)"; my $statementHandle = $databaseHandle->prepare($query); $statementHandle->execute($user, $comment); print "<h2> SUCCESS -- inserted into the DB! </h2>"; # If the SQL fails, we won't necessarily know. Check here. # Any errors? Print them here print "<p>Errors, if any: $DBI::errstr</p>"; # Close up $databaseHandle->disconnect(); $statementHandle->finish(); print end_html(); Note two question marks! Don’t worry about quotes around strings, execute() handles it!

Strict mode forces variables to be declared, and a few other things: Perl “strict” mode Strict mode forces variables to be declared, and a few other things: my $ii = 0; my @someArray = (1,2,3); Required for all IT452 Perl scripts Will save you pain! Also use “Carp” mode shown in examples Sends errors correctly to the client.

Example from before – what needs to change? Change URL Add “?” + data // Make synchronous call to server to get data for a new row function handleQuery() { xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); // Get data from server xhr.open("GET", "dummy_data1.csv", false); xhr.send(null); // GET, so no "data" part // Deal with results if (xhr.status != 200) { alert("Error contacting server! Status: "+xhr.status); } else { // Get comma-separated data and make into an array var data = xhr.responseText; var elems = data.split(",") // Make new row with this data insertRow(elems); return false; // false prevents the form from actually submitting