Skip to main content
March 26, 2008
Question

Loosing / in sql

  • March 26, 2008
  • 2 replies
  • 389 views
Im using cfdirectory to read a directory, and then adding the filename, size, directory into mysql 5 database.

Code:


<cfdirectory action="list" directory="D:\Backup\Cv'z" name="files" sort="Name desc" recurse='true' >
<cfset a = 1>
<cftable query="files" colheaders="yes" htmltable="no" border="yes" maxrows="10">
<cfcol text="#a#" >
<cfcol text="#type#">
<cfcol text="#size#">

<cfquery datasource="Intranet" name="addcv">
Select * from files
where File_Name = '#name#'
and File_Folder = '#directory#'
</cfquery>
<!--- Inserts file into DB ---->
<cfquery datasource="Intranet" name="insertCV">
Insert into files
(File_Name, File_Folder, File_Size)
values
('#name#', '#directory#', '#size#')
</cfquery>
<cfif addcv.recordcount gt 0>

<cfcol text="<font color=red>#name#</font>">
<cfelse>
<cfcol text="#name#">
</cfif>

<cfcol text="#directory#\#name#">
<cfset a = a + 1>

</cftable>




It inserts D:BackupCv'zSalesTyped Cvs

instead of d:\BackupCv's\Sales\Typed CVs

Why is it removing the \'s ?
This topic has been closed for replies.

2 replies

Inspiring
March 26, 2008
yes, \ [backslash] in mysql is an 'escape character' - a special
character used to escape other special characters in strings and string
patterns. to use an actual \ character in a string you must also escape it.
<cfqueryparam> tag DOES properly escape this mysql escape character, so
use it.

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
Inspiring
March 26, 2008
I'm not familiar with MySql but in some languages you must escape slashes used in strings.

One or both of these:
1. Use cfqueryparam in your insert statement, it will properly escape single quotes so it may also handle the slashes for you.

2. Escape the slashes by doubling them "d:\BackupCv's\Sales\Typed CVs" becomes: d:\\BackupCv's\\Sales\\Typed CVs"