Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Mysql error near values(

by GhodMode (Pilgrim)
on Feb 05, 2006 at 11:36 UTC ( [id://528057]=note: print w/replies, xml ) Need Help??


in reply to Mysql error near values(

It's hard to find the error because your code is all on one line, but it looks like the problem is that your URL isn't quoted. That must be the problem.

Using placeholders might make it easier for you to code if you are using a loop to execute the statements, but it might not give you a performance benefit in MySQL. MySQL doesn't support server-side prepared statements by default and I think that's where the performance benefit comes in with placeholders and bind values. I've had some problems with turning on this setting on, but try it if you want to experiment with it. ref: DBD::mysql (search for "prepared").

Columns with character data types will need quotes. You can quote everything, though because it won't break anything with the other data types.

MySQL will tell you what's wrong if you set up your queries properly. Break it back out to more lines during troubleshooting, then condense it again when everything works.

Here's my suggested method:

use strict; use warnings; my $connect_string = "DBI:mysql:$CONFIG::database"; my $dbh = DBI->connect( $connect_string, $CONFIG::dbusername, $CONFIG::dbpassword, \%CONFIG::dbattr ); my $statement; my $result; { package CONFIG; %dbattr = ( PrintError => 0, ChopBlanks => 0, ); $database = "stuff"; $dbusername = "username"; $dbpassword = "password"; } my $insert = qq{ INSERT INTO storage ( url, altavista, yahoo, msn, teoma, google, alltheweb, Total, lastsearch, totalsearch ) values( '$url', '$altavista_results', '$yahoo_results', '$msn_results', '$teoma_results', '$google_results', '$alltheweb_results', '$total', '$time', '$total' ) }; my $update = qq{ UPDATE storage SET url = "$url", altavista = "$altavista_results", yahoo = "$yahoo_results", msn = "$msn_results", teoma = "$teoma_results", google = "$google_results", alltheweb = "$alltheweb_results", total = total +1, time = "$time" }; $statement = $update; $result = $dbh->do( $statement ); if ( ! defined $result ) { die "Fatal SQL error :\n$statement\n" . $dbh->errstr; } # If there were no rows affected by the update, execute the # insert if ( $result < 1 ) { $statement = $insert; $result = $dbh->do( $statement ); if ( ! defined $result ) { die "Fatal SQL error :\n$statement\n" . $dbh->errstr; } }
--
-- GhodMode

Replies are listed 'Best First'.
Re^2: Mysql error near values(
by reneeb (Chaplain) on Feb 05, 2006 at 22:55 UTC
    Please use the placeholders!
    What if the google result contains a ' ?? Then your statement fails. Or use at least the $dbh->quote() method. It is much more security to your scripts.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-23 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found