Re: SQL - help and a little clarity

Your syntax is correct.

To do it from a perl program, you can do it in more ways. The one I use, 
because it also avoids SQL injections is:

my $dbh = DBI->connect("dbi:mysql:database=test", "user", "pass");

my $sth = $dbh->prepare("select * from table_name where name=? and date=? and 
time=?");
$sth->execute($name, $date, $time);

while (my $row = $sth->fetchrow_hashref) {
print qq~
name: $row->{name}
email: $row->{email}
date: $row->{date}
~;
}

or you can loop using other data collections than a hash reference:

array:

while (my @row = $sth->fetchrow_array) {
print qq~
name: $row[0]
email: $row[1]
date: $row[2]
~;
}

array reference:

while (my $row = $sth->fetchrow_arrayref) {
print qq<
name: $row->[0]
email: $row->[1]
date: $row->[2]
>;
}

I've wrote this code directly in OE, so it is not tested.
And, don't use the ";" in your SQL commands as you use it in the mysql command 
line client.

As you can see, in Perl you can include in strings array and hash elements, but 
you could also dereference and include hash reference and array reference 
elements.

Octavian

----- Original Message ----- 
From: "Lamar Upshaw" <lupshaw@xxxxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Monday, July 28, 2008 9:45 AM
Subject: SQL - help and a little clarity


>I am trying to have MySQL give information based on multiple conditions 
> being met. I remember how to do it with one:
> select * from table where username='tigger';
> 
> But I'd like to do it with more conditions, something like this:
> 
> select * from table where username='tigger' and date='2008-07-28' and 
> time='11:41:56';
> 
> Can someone assist me with what would be the correct syntax? I know this is 
> a simple one, but it's escaping me at the moment.
> 
> PS. I'm using perl to connect to the MySQL database using the DBI().  I'm 
> not sure what difference that will make, except to say that the three 
> conditions that need to be met will be from variables passed on from perl.
> 
> Thanks for any help.
> 
> With All Respect,
> Upshaw, Lamar T 
> 
> __________
> View the list's information and change your settings at 
> http://www.freelists.org/list/programmingblind
>
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind

Other related posts: