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

Slides:



Advertisements
Similar presentations
PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.
Advertisements

LIS651 lecture 3 taming PHP Thomas Krichel
Mod_perl High speed dynamic content. Definitions Apache – OpenSource httpd server Perl – OpenSource interpreted programming language mod_perl – OpenSource.
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.
PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
MySQL Access Privilege System
NEVUG - August 2004 To Boldly Go: Using Perl and the Perl DBI as an Adjunct to MS Access for Custom Reports Maggie Rioux MBLWHOI Library Woods Hole, Mass.
WaveMaker Visual AJAX Studio 4.0 Training
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.
W EB A PPLICATION D EVELOPMENT A PPLICATION T O B IO -I NFORMATICS -III Vicky Khanna M-Tech Bioinformatics.
PHP and MySQL Database. Connecting to MySQL Note: you need to make sure that you have MySQL software properly installed on your computer before you attempt.
Web Database Programming Connecting Database to Web.
Implementing Session Support COEN 351. State Maintenance Client Side Mechanisms  Cookies Client needs to allow cookies Cookie handling done by browser.
MSc Bioinformatics 2006 Biocomputing Databases and Perl 13 th February 2006.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.
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.
Perl DBI Scripting with the ILS Roy Zimmer Western Michigan University.
 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.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
Set 5: Perl and Database Connections
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
INTERNET APPLICATION DEVELOPMENT For More visit:
Survey of Advanced Perl Topics Database access "OpSys"-like functions Signal Handling Inside-Out Objects.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
27/07/2001 Copyright 2001, Munica Corporation, 1 How to write a DBD driver Daini Xie Strathy President Munica Corporation
DBI tutorial February 5 th, What is DBI DBI is short for Database Interface, which is a Perl module DBI is short for Database Interface, which is.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Lecture 10 – MYSQL and PHP (Part 2)
(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.
PHP with MySQL 1.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
How to create a simple Web application with CGI::Application Template::Toolkit and DBIx::Class Leonard Miller February 7, 2009.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
ITN Wake Tech1 ITN270 Advanced Internet Databases Lecture 11. The Perl DBI API - Part II Topics: – Perl DBI interface to MySQL.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
PHP and SQL Server: Connection IST2101. Typical web application interaction (php, jsp…) database drivers 2IST210.
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class Four Instructor:
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class Three Instructor:
 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 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CHAPTER 10 PHP MySQL Database
>> PHP: MySQL & CRUD. R ecall Database Tables Records is composed of Operations (CRUD) Create Retrieve Update Delete DBMS Access Control MySQL phpMyAdmin.
CSC 2720 Building Web Applications Accessing MySQL from PHP.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
Mysql YUN YEO JOONG. 1 Connecting to and Disconnecting from the Server 1 Connecting to and Disconnecting from the Server shell> mysql – h host -u user.
DBI: The Neophyte's Guide1 What is DBI? DBI = DataBase Interface DBI is database-independent DBI allows you to write code that interacts with databases.
PHP and SQL Server: Connection IST 210: Organization of Data IST2101.
COMP234 - perl Perl DBI Topics Database vs access methods DBMS and DBMS API's Relational database SEQUEL Perl DBI SQL.
1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in PHP consists of a number of functions.
Please remember to thank our sponsors!
Chapter 7 - Introduction to Common Gateway Interface (CGI)
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Avoiding SQL Injection
Objectives Connect to MySQL from PHP Learn how to handle MySQL errors
Perl Database – Just Enough
Advanced Topics Web Programming.
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Web Programming– UFCFB Lecture
Presentation transcript:

Programming MySQL, Perl COEN 351

Reading List Paul DuBois: MySQL and Perl for the Web, New Riders, 2002 Jacqueline D. Hamilton: CGI Programming 101, 2004

Checking Your Installation Use this script to check whether cgi and dbi are installed:script You should see mysql among the output. #! c:/Perl/bin/perl.exe use CGI; use DBI; $cgi=new CGI; print "CGI object was successfully = DBI->available_drivers(); print "These drivers are exit(0);

Connecting to the Database Create database and table In MySQL command line: CREATE DATABASE example1; USE example1; CREATE TABLE nlwest(name varchar(20), wins int, losses int); SHOW TABLES; DESCRIBE nlwest; INSERT INTO nlwest VALUES ('Los Angeles',13,8), ('San Francisco', 11, 8), ('San Diego',12,9), ('Arizona', 11, 11), ('Colorado', 9, 13); SELECT * FROM nlwest;

Connecting to the Database #! c:/perl/bin/perl.exe use strict; use DBI; my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none"); my $sth = $dbh->prepare("SELECT name, wins, losses FROM nlwest"); $sth->execute(); my $count=0; while( = $sth->fetchrow_array ()){ printf("name = %s, wins = %d, losses = %d\n", $val[0], $val[1], $val[2]); $count++; } print "$count teams total\n"; $sth->finish(); $dbh->disconnect(); DBI Class Method connect $dbh = DBI->connect($data_source, $username, $password) or die $DBI::errstr; $dbh = DBI->connect($data_source, $username, $password, \%attr) or die $DBI::errstr;

Connecting to the Database #! c:/perl/bin/perl.exe use strict; use DBI; my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none"); my $sth = $dbh->prepare("SELECT name, wins, losses FROM nlwest"); $sth->execute(); my $count=0; while( = $sth->fetchrow_array ()){ printf("name = %s, wins = %d, losses = %d\n", $val[0], $val[1], $val[2]); $count++; } print "$count teams total\n"; $sth->finish(); $dbh->disconnect(); DBI Class Method prepare $sth = $dbh->prepare($statement) or die $dbh->errstr; $sth = $dbh->prepare($statement, \%attr) or die $dbh->errstr; Prepares a statement for later execution by the database engine and returns a reference to a statement handle object.

Connecting to the Database #! c:/perl/bin/perl.exe use strict; use DBI; my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none"); my $sth = $dbh->prepare("SELECT name, wins, losses FROM nlwest"); $sth->execute(); my $count=0; while( = $sth->fetchrow_array ()){ printf("name = %s, wins = %d, losses = %d\n", $val[0], $val[1], $val[2]); $count++; } print "$count teams total\n"; $sth->finish(); $dbh->disconnect(); DBI Class Method execute $rv = $sth->execute or die $sth->errstr; $rv = or die $sth->errstr; Perform whatever processing is necessary to execute the prepared statement.

Connecting to the Database #! c:/perl/bin/perl.exe use strict; use DBI; my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none"); my $sth = $dbh->prepare("SELECT name, wins, losses FROM nlwest"); $sth->execute(); my $count=0; while( = $sth->fetchrow_array ()){ printf("name = %s, wins = %d, losses = %d\n", $val[0], $val[1], $val[2]); $count++; } print "$count teams total\n"; $sth->finish(); $dbh->disconnect(); DBI Class Method fetchrow_array () $ary_ref = $sth->fetchrow_arrayref; $ary_ref = $sth->fetch; # alias Fetches the next row of data and returns a reference to an array holding the field values. Null fields are returned as undef values in the array.

Connecting to the Database #! c:/perl/bin/perl.exe use strict; use DBI; my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none"); my $sth = $dbh->prepare("SELECT name, wins, losses FROM nlwest"); $sth->execute(); my $count=0; while( = $sth->fetchrow_array ()){ printf("name = %s, wins = %d, losses = %d\n", $val[0], $val[1], $val[2]); $count++; } print "$count teams total\n"; $sth->finish(); $dbh->disconnect(); DBI Class Method disconnect $rc = $dbh->disconnect or warn $dbh->errstr; Disconnects the database from the database handle. disconnect is typically only used before exiting the program. The handle is of little use after disconnecting. Sample Script

Alternative Row Retrieval Methods fetchrow_array() Returns an array fetchrow_arrayref() Returns a reference to an array fetchrow_hashref() Returns a reference to an hash

Alternative Row Retrieval Methods #! c:/perl/bin/perl.exe use strict; use DBI; use CGI qw(:standard :html3); print header(), start_html("MLB"); my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none"); my $sth = $dbh->prepare("SELECT name, wins, losses FROM nlwest"); $sth->execute(); = ('Team','Wins','Losses'); = while( = $sth->fetchrow_array ()){ } print table({-width=>'50%', -align=>'center'},caption(b('NL WEST')), ); print end_html(); $sth->finish(); $dbh->disconnect(); fetchrow_array () returns an array

Alternative Row Retrieval Methods #! c:/perl/bin/perl.exe use strict; use DBI; use CGI qw(:standard :html3); print header(), start_html("MLB"); my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none"); my $sth = $dbh->prepare("SELECT name, wins, losses FROM nlwest"); $sth->execute(); = ('Team','Wins','Losses'); = while( my $ref = $sth->fetchrow_arrayref ()){ } print table({-width=>'50%', -align=>'center'},caption(b('NL WEST')), ); print end_html(); $sth->finish(); $dbh->disconnect(); fetchrow_arrayref() returns reference to an array

Alternative Row Retrieval Methods #! c:/perl/bin/perl.exe use strict; use DBI; use CGI qw(:standard :html3); print header(), start_html("MLB"); my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none"); my $sth = $dbh->prepare("SELECT name, wins, losses FROM nlwest"); $sth->execute(); = ('Team','Wins','Losses'); = while( my $ref = $sth->fetchrow_hashref()){ $ref->{wins}, $ref->{losses}])); } print table({-width=>'50%', -align=>'center'},caption(b('NL WEST')), ); print end_html(); $sth->finish(); $dbh->disconnect(); fetchrow_hashref () returns reference to an hash

Letter-Case Variation #! c:/perl/bin/perl.exe use strict; use DBI; use CGI qw(:standard :html3); print header(), start_html("MLB"); my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none",{PrintError => 0, RaiseError => 1} ); my $sth = $dbh->prepare("SELECT name, wins, losses FROM nlwest"); $sth->execute(); = ('Team','Wins','Losses'); = while( my $ref = $sth->fetchrow_hashref( 'NAME_lc')){ $ref->{wins}, $ref->{losses}])); } print table({-width=>'50%', -align=>'center'},caption(b('NL WEST')), ); print end_html(); $sth->finish(); $dbh->disconnect(); MySQL is not case specific, but Perl is. When using field names, this parameter forces the names to be lower case. Use NAME-uc to force the names to be upper case.

High-Level Retrieval Methods Low level access uses prepare() execute() fetch loop finish() New versions of DBI have

High-Level Retrieval Methods #! c:/perl/bin/perl.exe use strict; use DBI; use CGI qw(:standard :html3); print header(), start_html("MLB"); my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none",{PrintError => 0, RaiseError => 1} ) or die "error"; = $dbh->selectrow_array("SELECT name, wins, losses FROM nlwest"); print "The leader is $row[0]" print end_html(); $dbh->disconnect(); selectrow_array returns a complete row in a Perl row context. selectrow_array returns the first column value in a Perl scalar context.

High-Level Retrieval Methods #! c:/perl/bin/perl.exe use strict; use DBI; use CGI qw(:standard :html3); print header(), start_html("MLB"); my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none",{PrintError => 0, RaiseError => 1} ) or die "error"; my $ref = $dbh->selectcol_arrayref ("SELECT name FROM nlwest"); print "The teams in order if defined ($ref); print end_html(); $dbh->disconnect(); selectcol_arrayref returns a complete column in a Perl row context. If the query has no result, it returns an empty list or undef

High-Level Retrieval Methods #! c:/perl/bin/perl.exe use strict; use DBI; use CGI qw(:standard :html3); print header(), start_html("MLB"); my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none",{PrintError => 0, RaiseError => 1} ) or die "error"; my $ref = $dbh->selectall_arrayref("SELECT name, wins, losses FROM nlwest"); = ('Team','Wins','Losses'); = foreach my $row_ref $row_ref->[1], $row_ref->[2] ])); } print table({-border=>'1', -width=>'50%', -align=>'center'},caption(b('NL WEST')), ); print end_html(); $dbh->disconnect(); selectall_arrayref returns all values as a matrix. All methods die if RaiseError is enabled

Modifying the database do() To issue a query that does not return rows do() returns number of rows affected or undef for an error. If RaiseError is enabled, you do not need to check

Modifying the database #! c:/perl/bin/perl.exe use strict; use DBI; use CGI qw(:standard :html3); print header(), start_html("MLB"); my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none",{PrintError => 0, RaiseError => 1} ) or die "error"; $dbh->do ("INSERT INTO nlwest (name, wins, losses) VALUES('San Francisco',12,8)" ); my $ref = $dbh->selectall_arrayref("SELECT name, wins, losses FROM nlwest"); = ('Team','Wins','Losses'); = foreach my $row_ref $row_ref->[1], $row_ref->[2] ])); } print table({-border=>'1', -width=>'50%', -align=>'center'},caption(b('NL WEST')), ); print end_html(); $dbh->disconnect(); do() to insert a row

Modifying the database #! c:/perl/bin/perl.exe use strict; use DBI; use CGI qw(:standard :html3); print header(), start_html("MLB"); my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none",{PrintError => 0, RaiseError => 1} ) or die "error"; $dbh->do ("INSERT INTO nlwest (name, wins, losses) VALUES('San Francisco',12,8)" ); my $ref = $dbh->selectall_arrayref("SELECT name, wins, losses FROM nlwest"); = ('Team','Wins','Losses'); = foreach my $row_ref $row_ref->[1], $row_ref->[2] ])); } print table({-border=>'1', -width=>'50%', -align=>'center'},caption(b('NL WEST')), ); print end_html(); $dbh->disconnect(); do() to insert a row

Modifying the database use DBI; use CGI qw(:standard :html3); sub updateWinsAndLosses { my ($dbh, $winner, $looser) $dbh->do ( "UPDATE nlwest SET wins = wins + 1 WHERE name = ?", undef, $winner); $dbh->do ( " UPDATE nlwest SET losses = losses + 1 WHERE name = ?", undef, $looser); } print header(), start_html("MLB"); my $dbh = DBI->connect ("DBI:mysql:host=localhost;database=example1", "root", "none",{PrintError => 0, RaiseError => 1} ) or die "error"; &updateWinsAndLosses($dbh, 'San Francisco', 'Los Angeles'); my $ref = $dbh->selectall_arrayref("SELECT name, wins, losses FROM nlwest"); = ('Team','Wins','Losses'); = foreach my $row_ref $row_ref->[1], $row_ref->[2] ])); } do() to change values