473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

MySQL apostrophy with LIKE won't work.

In table is record, field called CompanyName that has: Joe\'s Place (I
added the slashes before inserting the field.)

This will NOT work:
$x="Joe's Place";
$sql="Select * from Company where CompanyName LIKE '%" . addslashes($x) .
"%'";
(standard MySQL query code)

I don't get an error, but I don't get a hit either!

Here is the quirk. Take out the LIKE and put in an = and it works:

$sql="Select * from Company where CompanyName =' " . addslashes($x) . " ' ";
(standard MySQL query code)

Does anyone have any ideas of what is happening here?

Thanks,

Al

Jul 16 '05 #1
5 4372
uws
I <be**********@slb9.atl.mindspring.net>, Adams-Blake Co. skrev:
$sql="Select * from Company where CompanyName =' " . addslashes($x) . " ' ";
(standard MySQL query code)

Does anyone have any ideas of what is happening here?


Have you tried:

echo $sql = "SELECT ...";

and examining (or pasting in a commandline mysql session) the generated
query?

mvrgr, Wouter

--
uws mail uw*@xs4all.nl

i will hold you close :: if you're afraid of heights -- incubus
Jul 16 '05 #2
On Mon, 14 Jul 2003 08:09:27 -0700, "Adams-Blake Co."
<at************@adams.takeme.out.-blake.com> wrote:
Adams-Blake Co. wrote:
In table is record, field called CompanyName that has: Joe\'s Place (I
added the slashes before inserting the field.)

This will NOT work:
$x="Joe's Place";
$sql="Select * from Company where CompanyName LIKE '%" . addslashes($x) .
"%'";
(standard MySQL query code)

I don't get an error, but I don't get a hit either!

Here is the quirk. Take out the LIKE and put in an = and it works:

$sql="Select * from Company where CompanyName =' " . addslashes($x) . " ' ";
(standard MySQL query code)

Does anyone have any ideas of what is happening here?

Thanks,

Al

(responding to my own post)

There is what does work:
$sql="Select * from Company where CompanyName LIKE '%" .
addslashes(addslashes($x)).

In other words, you have to add TWO addslash functions.

Why?


mysql> select * from liketest where c = 'Joe\\\'s place';
+--------------+
| c |
+--------------+
| Joe\'s place |
+--------------+
1 row in set (0.00 sec)

mysql> select * from liketest where c like 'Joe\\\'s place';
Empty set (0.00 sec)

mysql> select * from liketest where c like 'Joe\\\\\'s place';
+--------------+
| c |
+--------------+
| Joe\'s place |
+--------------+
1 row in set (0.00 sec)

The manual explains:

http://www.mysql.com/doc/en/String_c...functions.html

"Note: Because MySQL uses the C escape syntax in strings (for example, `\n'),
you must double any `\' that you use in your LIKE strings. For example, to
search for `\n', specify it as `\\n'. To search for `\', specify it as `\\\\'
(the backslashes are stripped once by the parser and another time when the
pattern match is done, leaving a single backslash to be matched). Note:
Currently LIKE is not multi-byte character safe. Comparison is done character
by character. "

(Although why do you have the \ in your data anyway?)

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #3
On Mon, 14 Jul 2003 23:32:46 -0700, "Adams-Blake Co."
<at************@adams.takeme.out.-blake.com> wrote:
Because I used the "addslashes" function before I inserted the record. Isn't
that the correct way:

$CompanyName = "Joe's Place";
Insert into mytable fld1= addslashes($CompanyName)....

How else would you do it?


If you have:

Joe\'s place

... stored in the database, you've added slashes twice.

You should only add enough slashes so that the data gets to the database in
its original form.

If $CompanyName contains "Joe's Place" then doing one addslashes() as you say
is correct. This makes it "fld1='Joe\'s Place'" in the SQL, and stores "Joe's
Place".

However if it's already "Joe\'s Place" then another addslashes makes it
"fld='Joe\\\'s place'" in the SQL, and you store "Joe\'s Place" which wasn't
your original data.

Do you have one of the automatic escaping functions on, the magic_quotes*
settings? That would explain the double-escaping.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #4
Andy Hassall wrote:
On Mon, 14 Jul 2003 23:32:46 -0700, "Adams-Blake Co."
<at************@adams.takeme.out.-blake.com> wrote:
Because I used the "addslashes" function before I inserted the record. Isn't
that the correct way:

$CompanyName = "Joe's Place";
Insert into mytable fld1= addslashes($CompanyName)....

How else would you do it?


If you have:

Joe\'s place

... stored in the database, you've added slashes twice.

You should only add enough slashes so that the data gets to the database in
its original form.

If $CompanyName contains "Joe's Place" then doing one addslashes() as you
say
is correct. This makes it "fld1='Joe\'s Place'" in the SQL, and stores
"Joe's Place".

However if it's already "Joe\'s Place" then another addslashes makes it
"fld='Joe\\\'s place'" in the SQL, and you store "Joe\'s Place" which wasn't
your original data.

Do you have one of the automatic escaping functions on, the magic_quotes*
settings? That would explain the double-escaping.


Andy, et. al.

When I do add
$CompanyName = "Joe's Place";
Insert into mytable fld1= addslashes($CompanyName)....


and look at the field name in phpMySQLAdmin for the record I see: Joe\'s
Place. So I assume that the slash is actually stored in the database. And
this is why when I do:

$recsql="select CompanyName from mytable";
$rs = $db->Execute($recsql);
$cname= stripslashes($rs->Fields['CompanyName']);
(I use the ADODB wrapper)

Does the database table actually carry the slash? I don't know, but I see it
in MySQLAdmin.... so I figure I have to do the stripslashes. Everything
seems to work..... except when you do the "LIKE" search in SQL and you need 2
addslash functions.

I don't know if I have magic anything turned on. I know that the above code
works fine on my local Apache as well as whatever pair.com runs.

Let me know what you think regarding the double addslashes for Joe's Place.

-Al
Jul 16 '05 #5
Adams-Blake Co. wrote:
Andy Hassall wrote:
On Mon, 14 Jul 2003 23:32:46 -0700, "Adams-Blake Co."
<at************@adams.takeme.out.-blake.com> wrote:
Because I used the "addslashes" function before I inserted the record.
Isn't that the correct way:

$CompanyName = "Joe's Place";
Insert into mytable fld1= addslashes($CompanyName)....

How else would you do it?


If you have:

Joe\'s place

... stored in the database, you've added slashes twice.

You should only add enough slashes so that the data gets to the database
in
its original form.

If $CompanyName contains "Joe's Place" then doing one addslashes() as you
say
is correct. This makes it "fld1='Joe\'s Place'" in the SQL, and stores
"Joe's Place".

However if it's already "Joe\'s Place" then another addslashes makes it
"fld='Joe\\\'s place'" in the SQL, and you store "Joe\'s Place" which
wasn't your original data.

Do you have one of the automatic escaping functions on, the magic_quotes*
settings? That would explain the double-escaping.


Andy, et. al.

When I do add
$CompanyName = "Joe's Place";
Insert into mytable fld1= addslashes($CompanyName)....


and look at the field name in phpMySQLAdmin for the record I see: Joe\'s
Place. So I assume that the slash is actually stored in the database. And
this is why when I do:

$recsql="select CompanyName from mytable";
$rs = $db->Execute($recsql);
$cname= stripslashes($rs->Fields['CompanyName']);
(I use the ADODB wrapper)

Does the database table actually carry the slash? I don't know, but I see it
in MySQLAdmin.... so I figure I have to do the stripslashes. Everything
seems to work..... except when you do the "LIKE" search in SQL and you need
2 addslash functions.

I don't know if I have magic anything turned on. I know that the above code
works fine on my local Apache as well as whatever pair.com runs.

Let me know what you think regarding the double addslashes for Joe's Place.

-Al

OK, I FOUND THE ANSWER. It was Andy who helped me see the light here... along
with some other posts in the archives. It seems that "addslashes" does what
it says, but that MySQL strips them out before it pops the field in the
database. Under normal conditions it is NOT stored as Joe\'s Place but as
Joe's Place.

HOWEVER, if for some (dumb) reason you have something called
"magic-quotes-gpc" turned ON in your php.ini file, the slashes are added for
you atomatically. If you continue to do a "addslashes" you end up with
"Joe\\'s Place". MySQL strips out the first one, but leaves the second which
is why you will see the \ in the database if you go in and edit a record.

And if this is the case then you NEED to do a stripslashes when getting the
record in order to get rid of the darn \.

THE KEY, (IMO... and maybe I'm wrong) is to turn OFF this "magic quote"
thingy, and ALWAYS use the addslashes function on all strings that are going
to be inserted into SQL or if you are going to do a string search (select).

Maybe someone can explain the concept of this "magic quote" parm, but it
seems to me (and other postings that I've read) that the PHP developers made
a mistake by trying to do "too much" for the developer.... but I guess that's
another issue.

I hope someone will please come on and tell me if the above is a correct
analysis in case I'm all wrong. We don't want bad info to be on Google
without someone setting it straight because I'm sure others will have this
problem as well.

Al

Jul 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: NotGiven | last post by:
I am researching the best place to put pictures. I have heard form both sides and I'd like to know why one is better than the other. Many thanks!
9
by: elyob | last post by:
Hi, I'm looking at storing snippets of details in MySQL about what credit cards a business excepts. Rather than have a whole column for Visa, another for Amex etc ... I am looking at having a...
20
by: John Wells | last post by:
Yes, I know you've seen the above subject before, so please be gentle with the flamethrowers. I'm preparing to enter a discussion with management at my company regarding going forward as either...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
3
by: the.natalie | last post by:
Hi. I am a newbie to mysql, cron, and shell scripting, so please bear with me. I have a script that is used for updating an image directory based on contents in a database. The script does the...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
4
by: Vanessa | last post by:
Hi there I am an Access developer, and I have written applications for a 30 telephone call center, using the standard multiuser jet engine, it all works fine, but I want to move our systems onto...
7
by: Randy | last post by:
Folks: We have a web-based app that's _really_ slowing down because multiple clients are writing their own private data into a single, central database. I guess the previous programmer did...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.