PEPPLER.ORG
Michael Peppler
Sybase Consulting
Menu
Home
Sybase on Linux
Install Guide for Sybase on Linux
General Sybase Resources
General Perl Resources
Freeware
Sybperl
Sybase::Simple
DBD::Sybase
BCP Tool
Bug Tracker
Mailing List Archive
Downloads Directory
FAQs
Sybase on Linux FAQ
Sybperl FAQ
Personal
Michael Peppler's resume

sybperl-l Archive

Up    Prev    Next    

From: mpeppler at itf dot ch (Michael Peppler)
Subject: Re: char or varchar
Date: Feb 27 1996 8:23AM

> From: "Angie Lin" 
> 
> Hmm, still having problems implementing this.  Here's my simple scenario:
> 
> $tmp = "It's a small world"
> $db->dbcmd ("insert into foo (column1) values ($tmp)");
> 
> Now this by itself will generate:
> 	insert into foo (column1) values (It's a small world)
> 
> so, I've been manually putting quotes on via:
> 	$tmp = "'" . $tmp . "'"
> 
> but this generates:
> 	insert into foo (column1) values ('It's a small world')
> 
> which gives me a database error.  I've tried the following by itself:
> 	$tmp = $db->dbsafestr($tmp)
> 
> but that gave me:
> 	insert into foo (column1) values (It''s a small world)
> 
> So then I did both:
> 	$tmp = "'" . $tmp . "'"
> 	$tmp = $db->dbsafestr($tmp)
> 	$dbcmd ("insert ..... ")
> 
> but that just gave me:
> 	insert into foo (column1) values (''It''s a small world'')
> 
> Obviously I'm doing something wrong here.  Any suggestions?

Decide which quote type (single ' or double ") you want to use around
your strings, and then "protect" these quotes using dbsafestr():

$tmp = "It's a small world";   # Initial string.
$tmp = $db->dbsafestr($tmp, "'");   # Make single quotes safe.
$db->dbcmd("insert foo(column1) values('$tmp')");

Which should give you:
      insert foo(column1) values('It''s a small world')

An alternate solution:

Make sure all double quotes are converted to single quotes in your $tmp
variable, and then use double quotes in the SQL insert statement:

$tmp =~ s/"/'/g;
$db->dbcmd("insert foo(column1) values(\"$tmp\")");
                                       ^^^^^^^^^
                                       Need to escape the double quotes here

Hope this isn't too confusing...

Michael