Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$ARGV[0] in mysql query

Status
Not open for further replies.

macubergeek

IS-IT--Management
Dec 29, 2004
41
US
I'm having a problem with line 26. As you can see I'm trying to pass the value of Port as $ARGV[0] on the command line:

------------
CODE
1 #!/usr/bin/perl
2 ################
3 ##
4 ################
5 use DBI;
6 use strict;
7
8 ###############
9 ## Variables ##
10 ###############
11 my ($dsn) = "DBI:mysql:lookup:localhost";
12 my ($user) = "root";
13 my ($password) = "mysql";
14 my ($dbh,$sth);
15 my (@ary);
16
17 #########################
18 ## Connect to Database ##
19 #########################
20 $dbh = DBI->connect ($dsn, $user, $password, {RaiseError => 1});
21 ################
22 ## Make Query ##
23 ################
24 $sth = $dbh->prepare (qq{SELECT * FROM ports WHERE Port VALUES(?)});
25
26 $sth->execute("$ARGV[0]");
27 #####################################
28 ## Read and print results of query ##
29 #####################################
30 while (@ary = $sth->fetchrow_array ())
31 {
32 print join ("\t", @ary), "\n";
33 }
34 $sth->finish ();
35 ##############################
36 ## Disconnect from Database ##
37 ##############################
38 $dbh->disconnect ();
39 exit (0);
------------

Perl is complaining:
C:\Documents and Settings\kellyji\Desktop>perl port.pl 23
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the m
anual that corresponds to your MySQL server version for the right syntax to use
near 'VALUES('23')' at line 1 at port.pl line 26.
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the m
anual that corresponds to your MySQL server version for the right syntax to use
near 'VALUES('23')' at line 1 at port.pl line 26.

I wonder if someone can point out how I can correct this...like I've said, line 26 seems to be the culprit but I'm also a little leery of line 24 as well.
 
Why on earth put $ARGV[0] in quotes?
Try it without and i bet the problem goes away.

Trojan.
 
Surely
Code:
$sth = $dbh->prepare (qq{SELECT * FROM ports WHERE Port VALUES(?)});
ought to be
Code:
$sth = $dbh->prepare (qq{SELECT * FROM ports WHERE Port = ?});
I don't think the quotes would make a difference in this case, as you are using placeholders.
 
nope
removing the quotes gives me:
C:\Documents and Settings\kellyji\Desktop>perl port.pl 23
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the m
anual that corresponds to your MySQL server version for the right syntax to use
near 'VALUES ('23')' at line 1 at port.pl line 26.
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the m
anual that corresponds to your MySQL server version for the right syntax to use
near 'VALUES ('23')' at line 1 at port.pl line 26.
 
Aha figured it out:

this works:
$sth = $dbh->prepare (qq{SELECT * FROM ports WHERE Port = (?)});
$sth->execute ($ARGV[0]);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top