Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Mysql Select Query by Perl

by aaron_baugher (Curate)
on Jun 22, 2012 at 12:41 UTC ( [id://977838]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Mysql Select Query by Perl
in thread Mysql Select Query by Perl

No, that's backwards. In SQL (at least in MySQL), you need to quote string values, not column names. A statement like this:

SELECT * FROM mytable WHERE customer = Smith;

tries to find rows where the value of the customer field equals the value of the Smith field. So it gives you an error like the one you got, complaining that there isn't a column named Smith. If you're looking for a customer whose name is Smith, you do:

SELECT * FROM mytable WHERE customer = 'Smith';

In Perl/DBI, you can do that a couple different ways. The second one below, using placeholders, is much safer, especially when searching on user-provided data (which is the case most of the time). It's also easier, because you let DBI do the quoting for you.

my $name = 'Smith'; my $st = $db->prepare( qq| SELECT * FROM mytable WHERE customer = '$na +me'; | ); $st->execute or die $DBI::errstr; # or with placeholders my $name = 'Smith'; my $st = $db->prepare( q| SELECT * FROM mytable WHERE customer = ?; | +); $st->execute($name) or die $DBI::errstr;

Aaron B.
Available for small or large Perl jobs; see my home node.

Replies are listed 'Best First'.
Re^4: Mysql Select Query by Perl
by endymion (Acolyte) on Jun 22, 2012 at 13:08 UTC
    Thanks so much Aaron ! With your descrition I get the information in my head :-) My mistake was not to quote '%s', I directly quoted '$sel'. *lol*

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://977838]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-24 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found