[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 # The following section (uncommented) will create a database, 2 # user, and table for use in this script. 3 # 4 # echo "create database unattended;" | mysql 5 # echo "grant all on unattended.* to username@'%' identified by 'password';" | mysql 6 # echo "CREATE TABLE unattended ( 7 # Lookup varchar(128) NOT NULL default '', 8 # Property varchar(128) NOT NULL default '', 9 # Value varchar(255) NOT NULL default '', 10 # PRIMARY KEY (Lookup,Property) 11 # ) TYPE=MyISAM;" | mysql unattended 12 # 13 # The lookup_value function will query the above table when 14 # passed a Lookup, Property pair and return Value. If Value 15 # is 'Empty' then '' is returned. All errors return undef. 16 17 use strict; 18 use warnings; 19 use DBI; 20 use DBD::mysql; 21 22 # Must be named CONFIG 23 package CONFIG; 24 use Carp; 25 26 my $dbh; 27 28 # Setup connection to database 29 sub setup ($$$) { 30 my $self = shift; 31 my $dbconn = shift; 32 my $dbuser = shift; 33 my $dbpass = shift; 34 35 $dbh = DBI->connect($dbconn, $dbuser, $dbpass) 36 or undef $dbh; 37 } 38 39 # Query database looking up value for Lookup, Property pair 40 sub lookup_value ($$) { 41 my $self = shift; 42 my $lookup = shift; 43 my $property = shift; 44 45 defined $dbh 46 or return undef; 47 return undef 48 unless defined $lookup && defined $property; 49 # To avoid issues with different drivers return the field names in different cases, force to lower case 50 $dbh->{FetchHashKeyName} = 'NAME_lc'; 51 my $sth = $dbh->prepare("SELECT Value FROM unattended WHERE Lookup = '$lookup' and Property = '$property';") 52 or return undef; 53 $sth->execute 54 or return undef; 55 my $ref = $sth->fetchrow_hashref() 56 or return undef; 57 my $value = $ref->{'value'} 58 or return undef; 59 $value =~ /\S/ 60 or return undef; 61 print "Found $property for $lookup: $value\n"; 62 $value =~ /Empty/i 63 and return ''; 64 return $value; 65 } 66 67 # Make this file evaluate to "true". 68 1;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 17 22:47:18 2015 | Cross-referenced by PHPXref 0.7.1 |