bwern has asked for the wisdom of the Perl Monks concerning the following question:

I humbly submit the following conundrum for your consideration:

I am selecting several fields from a vanilla mysql database, however one row's result is being truncated. In particular, the 'torun' column contains <\ - this and anything following is truncated. For example, a database entry for torun contains:

touch /var/opt/OV/dbspi/defaults; /var/opt/OV/bin/instrumentation/dbsp +icfg -i </var/opt/OV/bin/instrumentation/disney-dbtab; /var/opt/OV/bi +n/instrumentation/dbspi_mw_int;
but the perl select:
$lookup = $dbh->prepare_cached("select file,whereto,filename,torun fro +m esm.ConfigMgr_FileRef"); $lookup->execute(); while ( ($Tfile,$Twhereto,$Tfilename,$Ttorun) = $lookup->fetchrow_arra +y) { print "$Twhereto\n"; print "$Tfilename\n"; print "$Ttorun\n"; }
returns only  touch /var/opt/OV/dbspi/defaults; /var/opt/OV/bin/instrumentation/dbspicfg -i

I validated that removing the < character allows the remaining text to be selected properly, but this is an important part of the data, which is used later in the program.

Despite searching perlmonks, nutshell books, and hitting my head against the keyboard, I have been unable to determine a fix. If any monk sees fit to share their knowledge, I would be eternally grateful.

Replies are listed 'Best First'.
Re: DBI mysql select truncating on special character
by graff (Chancellor) on Apr 18, 2006 at 22:59 UTC
    A couple things that come to mind (might not help, but just in case):

    If the printing is being done in the context of html or xml output (e.g. for display on a web page), then obviously you would need to convert all angle brackets and ampersands to their corresponding entity references before printing them (  s/\&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g;).

    (Update: rearranged the order of those substitutions -- ampersands need to be done first; of course, someone will probably recommend a module to do those replacements, which is probably a good idea.)

    Have you checked the length of "$Ttorun", in addition to printing its contents? (Is it actually longer when you remove the "<" from the database record, and shorter when the record contains "<"?)

      Graff, You were completely on the mark - when I checked the length it led me to the answer -- my string handling was truncating selected data becuase I wasn't handling quoting properly. Thanks for pointing me in the right direction!