Solved

pulling HTML text from SQL database using PERL/DBI

Posted on 2014-02-25
6
208 Views
Last Modified: 2014-03-03
Hi,
I'm using perl/dbi to query data from a SQL server database.
I've used perl and this DB dozens of times.
In this particular case, I am not getting anything back in the variable.
Here is the code:
 
my $dbh2 = DBI->connect('DBI:ODBC:MyDB', 'username',password', {PrintError=>0, RaiseError=>0} ) or die   "Can't connect: $DBI::errstr!";

my $hdata = $dbh2->prepare("SELECT sidedata FROM hsheetsave WHERE sheetid='2'");
$hdata->execute();
($query_headerdata) = $hdata->fetchrow_array;
$hdata->finish;
$dbh2->disconnect;

If I try to print out $query_headerdata I get nothing.
If I change the query to SELECT sheetid, then it works fine and I get a result, in this case 2.
The data that exists in sidedata is HTML markup. I verified that the data is there in the database by querying it using SQL directly on the server.
I'm thinking this issue might be that I'm pulling HTML markup out and  there is some encoding issue? I don't really know though.
Is there some special way I need to us DBI to pull HTML markup out of a SQL database?
Some help is very much appreciated.
Thanks,
Nacht
0
Comment
Question by:nachtmsk
  • 4
  • 2
6 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 39886302
When you queried the db from the cli, did you use the exact same sql statement as the one used in the script?

Why did you disable the error reporting in your sql statements?  It's possible that the prepare or execute statement failed and since you're not doing any error handling, you won't know if those statements fail.
0
 

Author Comment

by:nachtmsk
ID: 39886332
Yes, I used the exact same statement. In fact, I copy/pasted the statement from the code into the SQL window to run it. This is something I usually do when I have a problem similar to this. Usually it shows me something in wrong with the SQL, but in this case the SQL ran perfectly in the SQL window.

As for the error checking, your right, it should be enabled. If I enable it now though and I refresh the page that is calling this script, the page never draws. If I leave it at 0, the page at least draws, but not with any data from the script. Ah, ha!  I guess I can run this script from the command line to see what errors might be coming back when I enable the error reporting to a higher level.
0
 

Author Comment

by:nachtmsk
ID: 39886349
Fishmonger,
So I ran the script from the command line with error reporting at a 9.
I got an error, imagine that!

fetchrow_array failed:  String data, right truncation at test.pl


So, why would I be getting a truncation error if I'm querying data? I would understand if I was trying to put it into the database, but it's already in.

What do you think?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 39886417
How is that db field defined and what's the total length of data being stored in that field?

Take a look at the DBI attributes LongTruncOk and LongReadlen

Are you running this on Windows?  There are a couple of related ODBC adjustments that can be set to fix this issue, but I'm not exactly sure what adjustments need to be made.
0
 

Author Comment

by:nachtmsk
ID: 39886424
Something else I noticed.
The data type for the sidedata field in SQL is 'text'. If I change it to varchar(xx), then it works. But it doesn't work if I change it to varchar(MAX). Stranger and stranger....
0
 

Author Comment

by:nachtmsk
ID: 39886431
Fishmonger, I think our posts crossed each other.
Yes, I am running on Windows using ODBC. I will check the config of that.
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

706 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

20 Experts available now in Live!

Get 1:1 Help Now