User Tools

Site Tools


classes:general:dbsamples:perl_mysql

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
classes:general:dbsamples:perl_mysql [2020/10/23 17:18] – created localadminclasses:general:dbsamples:perl_mysql [2021/09/17 17:27] (current) localadmin
Line 1: Line 1:
 +====== Perl example for MySQL database connection ======
 +
 +<code>
 use strict; use strict;
 use DBI qw(:sql_types); use DBI qw(:sql_types);
Line 16: Line 19:
 $db_conn_string = "dbi:mysql:$db_name:$db_host:$db_port"; $db_conn_string = "dbi:mysql:$db_name:$db_host:$db_port";
  
-# Connect to db on Hrothgar+# Connect to db 
 if ($conn = DBI->connect($db_conn_string, $db_user, $db_pwd)) if ($conn = DBI->connect($db_conn_string, $db_user, $db_pwd))
 { {
   # Prepare and execute an SQL statement   # Prepare and execute an SQL statement
-  $sth = $dbh->prepare("SELECT table_name.column_name +  $stmt = $conn->prepare("SELECT table_name.column_name 
-                        FROM table_name +                          FROM table_name 
-                        WHERE table_name.field=something"); +                          WHERE table_name.field=something"); 
-  $sth->execute;+  $stmt->execute;
  
-  # retrieve the results+  # Retrieve the results
  
   print "Query Output \n";   print "Query Output \n";
-  while(my $ref = $sth->fetchrow_hashref() ) {+  while(my $ref = $stmt->fetchrow_hashref() ) {
  print $ref->{'column_name'};  print $ref->{'column_name'};
  print "\n";  print "\n";
Line 35: Line 38:
 else else
 { {
-  # Couldn't connect to MySQL db+  # Error! Couldn't connect to MySQL db
 } }
 +</code>
classes/general/dbsamples/perl_mysql.1603491528.txt.gz · Last modified: 2020/10/23 17:18 by localadmin