Re: fetch the column/rows and fields into hash from dbi



I can give you some example code that I have if that will help. I'll
try to comment on it a little more as well. Mine is from just one table
though. This is part of my "search.cgi" file that can search the
database (my one table) using several different fields.

#!/usr/bin/perl
use DBI; #use the DBI library in perl to interact with the database. In
this case MySQL
print "Content-type: text/html\n\n"; #required for the webpage to print

## Connect to the Database, if we fail, print why ##
$dbh = DBI->connect('DBI:mysql:my_db:localhost:3306', 'db_user',
'db_pass', { PrintError=>1, RaiseError => 1})
or die "Connecting: $DBI::errstr\n";

#####################
## Serial Number ##
#####################

if ($Form{scan_serial_num} != "")
{

## Search the Database only using the Serial Number (Primary Key) ##
$search = "SELECT * FROM customer_info WHERE scan_serial_num =
'$Form{scan_serial_num}'"; #This gets the value the user entered in the
form
$sth = $dbh->prepare($search);
$sth->execute; #executes the query

## If search returned information, then print each field and its value
##
## Else tell user that the search returned nothing and to retry
##
## This is the part you're interested in I think
##
if($hashref = $sth->fetchrow_hashref)
{
%rethash = %{$hashref};
if ($debug) ## if debug is not 0 then print out all of the
fields in the database
{
foreach $key (keys %rethash){print
"\$rethash{$key}=$rethash{$key}"."<BR>\n";}
}
## prints the header and body of the new page with the data in it
&retprintheader;
&retprintbody;

}

else #prints the previous page with an error on it saying to try to
search again

{
&noretprintheader;
&printnoentry;
&noretprintbody;
}
exit;
}


In other words, after you get your data back into the hash using:
if($hashref = $sth->fetchrow_hashref)
{
%rethash = %{$hashref};
Then you can specify either which key you want to get out of the hash
using $rethash{$key} where key is a field in your database such as
$owner. Or you can iterate through them using the loop I have for
debug. I hope this helps some at least.

.



Relevant Pages

  • Re: Newbie - Is this Reasonable?
    ... because this hash is stored in the database. ... So you use PKCS5v2 to generate a key hash from a salt and the user's passphrase, then store the salt and the hash in a database. ... are even more critical in database applications because the payoff from tampering with selected fields may be much higher, fields tend to be fixed-length so it's easier to tamper with them in a meaningful way, and databases lend themselves to off-line analysis, so the attacker can marshall more resources and take more time to attack your system. ... You're using a stream cipher for encryption. ...
    (sci.crypt)
  • Re: looking for help with a counting algorithm
    ... >> subcategory is counted, the code goes back up the tree to the root, adding ... >> involve retrieving all the category memberships from the database, ... sub ReadCategories{ ... ReadCategories is called with two empty hash pointers by any of the ...
    (comp.lang.perl.misc)
  • Re: Secure Password in database
    ... Subject: Secure Password in database ... > in database as SHA hash. ... You don't want to be able to compromise the client, ... get a bunch of garbage back when you try to get the 2-way encrypted data. ...
    (SecProg)
  • Re: out of memory
    ... read only the smaller file into a hash. ... the smaller file will fit into RAM. ... Depending upon the sorting algorithm this would be Ologor ... put your relevant data into a database and use ...
    (comp.lang.perl.misc)
  • Securing a website...storing hashed passwords?
    ... security for a web site that presumably won't have an SSL option. ... The server will supply a random value to a login page. ... with the random value and create an MD hash. ... database are obviously a nono. ...
    (comp.security.misc)