Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl sendmail and MYSQL

script not working with an WHERE clause

         

hafnius

12:23 am on Apr 20, 2004 (gmt 0)

10+ Year Member



Hi all

Been tampering to modify a .pl script that fetches email addresses from a database. I can get it to send the emails fine if i query it like this in the script:

my $sth = $dbh->prepare("SELECT email FROM persons");

But i need this to work as i have different languages to serve:


my $sth = $dbh->prepare("SELECT email FROM persons WHERE page = $page");

Is my syntax wrong - i think this should be right?

The $page is defined in the form that uses the script. And defined like this with all the other variables in the script:

my $page = $q->param('page');

Since i have only two languages to serve, would it be better to make an if - else for the value of $page and thus not setting it as a variable?

Hope this is clear - i didnt know how much of the scipt was relevant to show.

Kind Regards
/Hafnius

hafnius

12:33 am on Apr 20, 2004 (gmt 0)

10+ Year Member



Hmmm

Sorry bout that - just found the solution. I had forgotten two ''.

So the qeury should be like this to work:

my $sth = $dbh->prepare("SELECT email FROM persons WHERE page='$page'")

Instead of:

my $sth = $dbh->prepare("SELECT email FROM persons WHERE page = $page")

What a wonderfull feeling when this stuff worx.

Kind Regards
/Hafnius - master of PERL :)

Birdman

12:33 am on Apr 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the column page is a string type, like VARCHAR, TEXT etc, you have to surround the value with quotes. So try this instead:

my $sth = $dbh->prepare("SELECT email FROM persons WHERE page = '$page'");

Birdman

<added>you got it!</added>

markanthony

6:43 pm on May 17, 2004 (gmt 0)

10+ Year Member



You may have another problem....

Are you using dbi->quote()?
my $page = "whatever I have in here: maybe some!@#";
$page = dbh->quote($page);
my $sth = $dbh->prepare("SELECT email FROM persons WHERE page = $page")

You can also add a datatype argument but you will have to consult the DBI docs for that.