Toggle navigation Home Store Browse All ----- Webhosting Domains Quotation (Webdesign Services) eMail Hosting SEO- Search Engine Optimisation GRAPHIC DESIGN Dedicated/ VPS Webhosting BULKSMS Mobi Applications Website Maintenance Register a New Domain Transfer Domains to Us Announcements Knowledgebase Network Status Affiliates Contact Us Account Login Register ----- Forgot Password? Categories 9 CGI Scripts 34 Databases 28 Dedicated Servers 1 DNS 15 Domains 5 eCommerce 79 eMails 27 FTP 1 General Issues 1 MX Records 1 Nameservers 28 Other 104 Scripting 254 Web Hosting Control Panel 9 Web Resources 8 Web Stats & Logs 11 Web Tools 8 Website Help and Diagnostics 4 Windows Hosting Categories CGI Scripts (9) Databases (34) Dedicated Servers (28) DNS (1) Domains (15) eCommerce (5) eMails (79) FTP (27) General Issues (1) MX Records (1) Nameservers (1) Other (28) Scripting (104) Web Hosting Control Panel (254) Web Resources (9) Web Stats & Logs (8) Web Tools (11) Website Help and Diagnostics (8) Windows Hosting (4) [SQL2.01] How do I connect to my MySQL database from a Perl CGI script? Perl's DBI module provides an interface to MySQL databases for CGI scripts. A commented example can be found below:#!/usr/bin/perluse DBI;# Connect To Database# * The DBI interface to MySQL uses the method "connect" to make a# * connection to the database. It takes as it's first argument# * the string "DBI:mysql:database:hostname", where database is equal# * to the name of your database, and hostname to the server that it's# * located on. The second and third arguments, respectively, should# * be your account username and password. The connection is assigned.# * to a variable that is used by most other methods in the module.$database = "your database name";$username = "your database username";$password = "your database password";$hostname = "your database hostname";$db = DBI->connect("DBI:mysql:$database:$hostname", $username, $password);# Execute a Query# * executing a query is done in two steps. First,# * the query is setup using the "prepare" method.# * this requires the use of the variable used to# * initiate the connection. Second, the "execute"# * method is called, as shown below.$query = $db->prepare("SELECT * FROM test");$query->execute;# How many rows in result?# * the "rows" method using the variable name the# * query was executed under returns the number# * of rows in the result.$numrows = $query->rows;# Display Results# * the fetchrow_array method executed on the# * query returns the first row as an array.# * subsequent calls return the other rows in# * sequence. It returns zero when all rows have# * been retrieved.while (@array = $query->fetchrow_array) { ($field1, $field2, $field3) = @array; print "field1 = $field1, field2 = $field2, field3 = $field3 \n"; }# Cleaning Up# * with the DBI module, it is a good idea to clean up by# * explicitly ending all queries with the "finish" method,# * and all connections with the "disconnect" method.$query->finish;$db->disconnect;exit(0);The DBI Module provides other methods you might find useful. More information is available by running "perldoc DBI" while logged on to your account via SSH. Was this answer helpful? Yes No Print this Article Also Read [SQL2.02] How do I query a MySQL database from a PHP script? Use the following outline to connect and begin querying the MySQL server from within your PHP... [SQL2.04] How do I set up the MySQL connection using Dreamweaver? 1. Open Dreamweaver2. Click File>New3. Choose “Dynamic Page” in left frame and then “PHP” (or... [SQL2.08] How do I grant permissions to a MySQL standalone user? MySQL standalone users provide additional security in certain advanced applications of database... [SQL2.07] Can I use CocoaMySQL? Yes you can.Please ensure you are using CocoaMySQL 0.7b5 or later. Please note that CocoaMySQL... [SQL2.03] How do I connect to MySQL with ASP? This applies to Windows Hosting accounts only.You will need to use a DSNless connection which can... Support My Support Tickets Announcements Knowledgebase Downloads Network Status Open Ticket × Close Title Loading...