Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Using a MySQL database from Perl

Both Perl and MySQL are major pieces of open source software with a multitude of facilities available - yet you can use them together with just a tiny piece of software "glue" - the DBI and DBD::MySQL module pair from the CPAN (comprehensive Perl archive network).

Once you've got Perl and MySQL installed, the mysqld running, and DBI available to you, inserting data is as easy as connecting to the database:
$cid = DBI -> connect ("DBI:mysql:forsale:192.168.200.66","trainee","abc123");
and running your insert:
$cid->do ("Insert into agents (agent,phone) values ('$ARGV[0]','$ARGV[1]')");

Selecting and displaying records needs a couple more lines of code, since you have to step through ("iterate through") the result set very much like you would read through a file - necessary because the result set may be too big to process all at once. After connecting, prepare and execute your SQL SELECT command:
$action = $cid->prepare("Select agent,phone from agents");
$action->execute();

and you can then step through your result set row by row:
while (@row = $action->fetchrow()) {
print "Call $row[1] to reach $row[0]\n";}


Link - Complete source code of the example that I've quoted from.

We now cover interfacing Perl to databases such as MySQL, and Oracle and Sybase which work through the same mechanism, briefly on our learning to program in Perl course if any of our delegates with to interface to a database. We also cover the subject in more detail on our MySQL and Perl for Larger Projects courses.

A caution - if you're writing a program such as the example I've quoted here that will be used by people other than yourself, you need to add a little extra code to "clean" the user's input - check that it's data that you really want in your database, and that it's not formatted like a piece of malicious SQL to run what's known as an "injection attack"
(written 2006-03-13, updated 2006-06-09)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
S156 - Interfacing Applications to MySQL Databases
  [104] mysql_connect or mysql_pconnect in PHP? - (2004-10-30)
  [663] Python to MySQL - (2006-03-31)
  [723] Viewing images held in a MySQL database via PHP - (2006-05-17)
  [1381] Using a MySQL database to control mod_rewrite via PHP - (2007-10-06)
  [1450] Easy selection of multiple SQL conditions from PHP - (2007-11-30)
  [1518] Downloading data for use in Excel (from PHP / MySQL) - (2008-01-25)
  [1561] Uploading to a MySQL database through PHP - examples and common questions - (2008-03-02)
  [1885] Hiding a MySQL database behind a web page - (2008-11-15)
  [2263] Mysqldump fails as a cron job - a work around - (2009-06-30)
  [2381] Checking the database connection manually - (2009-08-28)
  [2745] Connecting Python to sqlite and MySQL databases - (2010-04-28)
  [2790] Joining a MySQL table from within a Python program - (2010-06-02)
  [3035] How to display information from a database within a web page - (2010-11-07)
  [3099] Perl - database access - DBD, DBI and DBIx modules - (2010-12-22)
  [3447] Needle in a haystack - finding the web server overload - (2011-09-18)
  [3455] MySQL, MySQLi, PDO or something else - how best to talk to databases from PHP - (2011-09-24)
  [4436] Accessing a MySQL database from Python with mysql.connector - (2015-02-21)

P308 - Using SQL Databases from Perl
  [515] MySQL - an FAQ - (2005-12-03)
  [975] Answering ALL the delegate's Perl questions - (2006-12-09)
  [1224] Object Relation Mapping (ORM) - (2007-06-09)
  [1904] Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23)
  [2561] The future of MySQL - (2010-01-03)


Back to
If it's Sunday, it must be Bedwyn
Previous and next
or
Horse's mouth home
Forward to
Lost Camel
Some other Articles
West Wilts Railway Users Group and trains to Lacock
Checking for MySQL errors
PHP - London course, Melksham Course, Evening course
Lost Camel
Using a MySQL database from Perl
If it's Sunday, it must be Bedwyn
How similar are two words
Simple but rugged form handling demo
Training Centre Pictures
Progress bars and other dynamic reports
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.info/mouth/644_Usin ... -Perl.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb