Solved

Using Perl and MySQL

Posted on 2002-06-03
7
254 Views
Last Modified: 2010-03-05
I have written Perl code to access a MySQL database.  I am changing the code to cache previously loaded web pages into a 4k text buffer in the database.  The internet response is loaded into a variable called $response as follows:
  $req=HTTP::Request->new("GET", $url);
  $req->content_type('application/x-www-form-urlencoded');
  $response=$ua->simple_request($req);

To save the information the following is done:
  $respstr = $response->as_string;
  $respstr1 = substr($respstr, 0, 4000);
  $cachepage = $respstr1;
  $sql = "update links set cachepage='$cachepage' where id=$id";
  $tda = $dbh->prepare($sql) or die "Error preparing: ", $dbh->errstr;
  $tda->execute or die "Error executing: ", $dbh->errstr;

The variable $cachepage is a text type in the MySQL database.  

When the execute is attempted, no response is returned and I am sure it fails since the database is not written to.
Do I need to set a length work or something to get this to work?  Thanks for any help.
0
Comment
Question by:mzehner
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Expert Comment

by:bluprint
ID: 7051898
$tda->execute or die "Error executing: ", $dbh->errstr;
$dbh->commit; ## <- you need to commit the update.
0
 
LVL 2

Author Comment

by:mzehner
ID: 7052391
It still does not work even with $dbh->commit;.  There is no error message, but unless the line:
$tda->execute or die "Error executing: ", $dbh->errstr;
is commented out the web page will not return output.
0
 
LVL 1

Expert Comment

by:bluprint
ID: 7052444
Silly question...but are you connecting to the DB first?

You also should escape the single quotes

$sql = "update links set cachepage=\'$cachepage\' where id=$id";

I'm pretty sure this is the case...I did a project with MySQL once so this is from memory, also I don't have immediate access to some tools such as perl... In any case, it won't hurt....

One other thing, you shouldn't need to prepare that update statement. Just do:

$sql = "update links set cachepage=\'$cachepage\' where id=$id";
$dbh->do($sql);
if($DBI::err != 0) {
  print "error here!\n";
  print $dbh->errstr;
}
$dbh->commit;

Maybe something in there will move you in the right direction...
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 4

Expert Comment

by:dda
ID: 7053203
Also add this to your script:

use CGI::Carp qw(fatalsToBrowser);

It will cause 'die' messages to go to your browser.
0
 
LVL 2

Author Comment

by:mzehner
ID: 7058046
bluprint:
I am connecting to the database first since I am performing successful queries prior to the code I posted above.

dda:
I tried your suggestion.  It worked with an interesting error.  I stated that I have an error in my SQL syntax near 'Gave information here'.
The interesting thing about the information is that it is part of the string that I am writing to the database.  This is why I suspect that it is a possible problem with the syntax of the variable being written.  I have not used blob or text types (beyond 255 characters) with MySQL before.

I removed:
$respstr = $response->as_string;
and the program worked responded with the text output I placed at the end of it.  It appeared to work, but I'm not sure the datbase contains the web page contents.

Therefore part of the problem was that I was converting it to a string before saving to the database.  I'm not sure why this caused a problem.  I still need to investigate to tell if the data was wriiten to the database.  It does not appear in MySQL admin.
0
 
LVL 4

Accepted Solution

by:
dda earned 100 total points
ID: 7058750
I bet you have single or souble quotes in yout text being inserted into database. You can mask them with '\' or, better way, use placeholders and parameters to execute():

$sql = "update links set cachepage=? where id=?";
$tda = $dbh->prepare($sql) or die "Error preparing: ", $dbh->errstr;
$tda->execute($cachepage, $id) or die "Error executing: ", $dbh->errstr;
0
 
LVL 2

Author Comment

by:mzehner
ID: 7065892
Thanks, your comments helped me debug and solve the problem.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 122
Perl script to parse xml file 2 96
Writing a parser for java language 4 61
Perl string filter 5 63
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now