Dear Monks,

I know the title it does not make sense, but this is my main question/problem.

The question evolves several parts and a lot of explanation so please bear with me as it will be a long one with a lot of analysis in between.

I have a working code with do commands and prepare. I know that do is exactly the same with prepare and execute together. I have read O'Reilly's CD bookshelfs/5.5. do( ) Versus prepare( ) that there is no difference between them, unless if you are going to apply multiple loops. Where in this case do will apply multiple requests in comparison to prepare which will execute all requests at once.

So I thought it would be a good idea to become more familiar with prepare and start using it more frequently and not only on INSERT,SELECT and UPDATE statements but everywhere. At this point there is my first problem. I am using prepare on the same piece of code that do works fine and although that I do not get any errors I do get a hash value not an integer. Sample of code provided under:

my $databases = $dbh->do("SHOW DATABASES LIKE '".$DATABASE."'") or die "Error: " .dbh->errstr. "\n"; my $databases = $dbh->prepare("SHOW DATABASES LIKE ? ") or die "Error: " .dbh->errstr. "\n"; $databases->execute($DATABASE) or die "Error: " . $databases->errstr. "\n"; print "This is the databases: ".$databases."\n";

Output of do command:

This is the databases: 1

Output of prepare command

This is the databases: DBI::st=HASH(0xe47058)

Complete working sample code can be found under:

#!/usr/bin/perl use strict; use warnings; use DBD::mysql; $|=1; #flush every time the program my $DATABASE = 'xxxx'; my $USERNAME = 'xxxx'; my $TABLE = 'xxxx'; my $HOST = 'xxxx'; my $PORT = 'xxxx'; my $PASS = 'xxxx'; my $checkExist; sub mysql { my $dbh = DBI->connect("dbi:mysql::".$HOST.":".$PORT."", "".$USERNAME."", "".$PASS."", { 'PrintError' => 1, 'RaiseError' => 1 } ) or die "Could not connect to ". $HOST .": ". $DBI::errstr ."\n"; my $databases = $dbh->do("SHOW DATABASES LIKE '".$DATABASE."'") or die "Error: " .dbh->errstr. "\n"; =comment my $databases = $dbh->prepare("SHOW DATABASES LIKE ? ") or die "Error: " .dbh->errstr. "\n"; $databases->execute($DATABASE) or die "Error: " . $databases->errstr. "\n"; =cut print "This is the databases: ".$databases."\n"; exit(); if ($databases eq 1) { printf "Database: ". $DATABASE ." exists not creating!\n"; } else { printf "Database: ". $DATABASE ." does not exist creating!\n"; $checkExist = $dbh->prepare("CREATE DATABASE IF NOT EXISTS `".$DAT +ABASE."`") or die "Could not create the: ".$DATABASE." error: ". $dbh->er +rstr ."\n"; if (!$checkExist->execute()) { die "Error: ". $checkExist->errstr ."\n"; } } # End of else $dbh->do("USE ".$DATABASE."") or die "Error: " .dbh->errstr. "\n"; my $tables = $dbh->do("SHOW TABLES FROM `".$DATABASE."` WHERE Tabl +es_in_".$DATABASE." LIKE '".$TABLE."'") or die "Error: " .dbh->errstr +. "\n"; print "Tables do: ".$tables."\n"; $tables = $dbh->prepare("SHOW TABLES FROM `".$DATABASE."` WHERE Ta +bles_in_".$DATABASE." LIKE '".$TABLE."'") or die "Error: " .dbh->errs +tr. "\n"; $tables->execute() or die "Couldn't execute statement: " . $tables->errstr; print "Tables prepare: ".$tables."\n"; if ($tables eq 1) { printf "Table: ".$TABLE." exists not creating!\n"; } else { printf "Table: ".$TABLE." does not exist creating!\n"; $checkExist = $dbh->prepare("CREATE TABLE IF NOT EXISTS `".$TABLE. +"` ( `id` int(11) NOT NULL AUTO_INCREMENT, `UnixTime` int(11) NOT NULL, `losses` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREM +ENT=1 ;"); if (!$checkExist->execute()) { die "Error: ". $checkExist->errstr ."\n"; } } # End of else my $range = 50; my $minimum = 100; my $random_number = int(rand($range)) + $minimum; my $time = time(); my $losses = $time . ' ' . $random_number; $checkExist = $dbh->prepare("INSERT IGNORE INTO `".$TABLE."` (`Uni +xTime`, `losses`) VALUES ('".$time."','".$random_number."') "); if (!$checkExist->execute()) { die "Error: ". $checkExist->errstr ."\n"; } $checkExist->finish(); $dbh->disconnect(); return $losses; } # End of mysql sub my $output = &mysql(); print "This is the output: ".$output."\n";

At this point I think the syntax is correct because if I only apply do it works just fine. So can someone elaborate me more why I can not get the same result with do and prepare?

Thank you all for your time and effort assisting me.

Seeking for Perl wisdom...on the process...not there...yet!

In reply to Can not get the same output from $dbh->do and $dbh->prepare by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.