Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

db connection help

by attagirl (Initiate)
on Dec 12, 2017 at 18:12 UTC ( [id://1205359]=perlquestion: print w/replies, xml ) Need Help??

attagirl has asked for the wisdom of the Perl Monks concerning the following question:

Hello, we have been asked to take over a website built using perl (it's a club site). The author is not available to help much, but has passed us on the contents of the db and the site.

At this point we just want (need) to transfer the site to our hosting company and then we will figure out where to go. The club administrator would like to get some yearend data in the db and then next year we can go at updating it as it's pretty outdated coding for the html.

We have the database set up and it seems ok. Originally the db was in postgresql. We converted it in workbench to mysql. Our provider has indicated the hostname to use is localhost.

We'd be really grateful if anyone could take a look and let us know if you see a problem with the code below. And if not suggest where we might look for clues. The file is a .pm file.

Thanks in advance. If anything else is needed please let us know.

Attagirl
sub DbConnect { print DEBUG "In DbConnect\n"; use Sys::Hostname; $Hostname=hostname; $MyDB="db######"; if($Hostname !~ /myurl/){ $User="root"; #just for local version. $PW=''; #just for local version. } else { $User="db######"; open(PASS,"$ProgOption{PasswordFile}"); $PW=<"######">; chomp $PW; } #print DEBUG " $ProgOption{dbh}=DBI->connect('DBI:mysql:host=localhos +t;database=$MyDB','$User','$PW',{PrintError =>0, RaiseError => 1})\n" +; $ProgOption{dbh}=DBI->connect("DBI:mysql:host=localhost;database=$My +DB","$User","$PW",{PrintError =>0, RaiseError => 1}); return $ProgOption{dbh}; }

Replies are listed 'Best First'.
Re: db connection help
by huck (Prior) on Dec 12, 2017 at 19:04 UTC
Re: db connection help
by 1nickt (Canon) on Dec 12, 2017 at 18:41 UTC

    Hi, your DBI connect statement looks wrong. Try this:

    my $DSN = "dbi:mysql:$MyDB"; $ProgOption{dbh}=DBI->connect($DSN, $User, $PW, {PrintError => 0, Rais +eError => 1 });

    Hope this helps!



    The way forward always starts with a minimal test.
Re: db connection help
by roboticus (Chancellor) on Dec 12, 2017 at 22:43 UTC

    attagirl:

    Never mind ... I was looking at your debug string, not the line that actually calls DBI->connect.

    Variables referenced inside single-quoted strings don't expand as you're expecting them to:

    my $foo = 'Password'; printit('Foo is $foo'); printit("Foo is $foo"); printit($foo); sub printit { my $val = shift; print "val=<$val>\n"; }

    If you run the above code, it'll give you:

    val=<Foo is $foo> val=<Foo is Password> val=<Password>

    As you can see, the single-quoted string isn't giving you what you want. If you want the value to expand, use double-quoted strings.

    So the first problem is that you're using single-quoted strings in places where a double-quoted string is more appropriate. (The first argument to the DBI->connect function). You're also using quoted strings where you don't even need them (the second and third arguments, for example).

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: db connection help
by attagirl (Initiate) on Dec 13, 2017 at 17:08 UTC
    thank you all for you help. We're plugging away trying to solve this and will let you know how this turns out or ask if we need further help. Attagirl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1205359]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-24 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found