0

I'm using DBI for the first time (and not long started Perl [2 weeks]) and I can't seem to get any results from the database. Here's what I have:

if( defined( $get{findAllPages} ) && defined( $post{ki} ) ){

   my ($database, $hostname, $port, $password, $user );

   $database = "#########";
   $hostname = "localhost";
   $password = "#########";
   $user = "###########";
   my $KI = $post{ki};

   # connect to the database
   my $dsn = "DBI:mysql:database=$database;host=$hostname;";
   my $dbh = DBI->connect($dsn, $user, $password);
   my $sth = $dbh->prepare("SELECT * FROM accounts WHERE KI = '" . $dbh->quote($KI) . "' ") or die "Could not select from table";
   $sth->execute();
   if( $sth->rows != 0 ) {
      my $ref = $sth->fetchrow_hashref();
      my $domain = $ref->{website};
      my $DB_username = $ref->{db_name};
      my $DB_password = $ref->{db_pass};
      $sth->finish();
      $dbh->disconnect();

      print "domian: " . $domain . "<br />\n";

      chomp(my $url = trim($domain));

As it stands it checks to see if KI is correct and then checks row amount which works. The bit I can't get to work is returning values from the array;

my $ref = $sth->fetchrow_hashref();
my $domain = $ref->{website};
my $DB_username = $ref->{db_name};
my $DB_password = $ref->{db_pass};

If anyone can let me know where I am going wrong it would be much appreciated.

2 Answers 2

2

Try to use DBI's error handling to see what's wrong. See Programming the Perl DBI, chapter 4 (Error handling) or "DBI::mysql error handling" on Perlmonks for references.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, could you tell me if "or die" works in online cgi files as I can never seem to get it to output to the browser.
fetch() without execute().. so im guessing... my $dsn = "DBI:mysql:database=$database;host=$hostname;"; my $dbh = DBI->connect($dsn, $user, $password); my $sth = $dbh->prepare("SELECT * FROM accounts WHERE KI = '" . $dbh->quote($KI) . "' ") or die "Could not select from table"; $sth->execute(); if( $sth->rows != 0 ) { my $ref = $sth->fetchrow_hashref(); $ref->execute(); print $DBI::errstr; my $domain = $ref->{website}; my $DB_username = $ref->{db_name}; my $DB_password = $ref->{db_pass}; $sth->finish(); $dbh->disconnect(); shuld do it
if you are using cgi, one method is to use CGI::Carp. see perldoc.perl.org/CGI/…. by the way, post your code in your question, not in the comments because the formatting is bad.
grrr... it works fine when I run from cmd on my local machine but when I use via browser tells me to do one thing, then when i go back to my local machine that change i made causes an error...
What does the browser tell you to do?
0

What I often do when I can't see what is going on:

use Data::Dumper;

my $ref = $sth->fetch();

print Dumper $ref;

and you'll see the data structure layout. Sometimes it is a matter of incorrect indexing into an array or hash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.