Forum Moderators: coopster

Message Too Old, No Replies

Apostrophe's in mysql query

         

harryhermit

6:02 am on Feb 7, 2004 (gmt 0)

10+ Year Member



Hello All,

I'm making a query on a column that has text data using:

mysql_query("SELECT * FROM products WHERE brand
like '%$pham_brand%' AND item!='$item' Order by rand() Limit 25 ")

For the most part it works. It displays items of the same brand, but when it comes across text with an apostrophe, it messes up and gives me an error. I understand why this happens and that I need to escape it, but I'm not sure of the functions I need to use or correct syntax.

I'm aware of the addslashes() function, but I haven't got it to execute successfully.

Someone told me to write it like this:

mysql_query("SELECT * FROM products WHERE brand
like 'addslashes($pham_brand)' AND item!='$item' Order by rand() Limit 25 ")

Well this doesn't work. Anyhow, I guess I could manually escape my text in the database, but we're talking about hundreds of entries. Any suggestions? Thanks!

Timotheos

7:25 am on Feb 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe this will work...
mysql_query("SELECT * FROM products WHERE brand like '%" . addslashes($pham_brand) . "%' AND item!='$item' Order by rand() Limit 25 ")

I'm just not sure if the string in the query is parsed. I usually build the statement before and then do the query.
$query = "SELECT * FROM products WHERE brand like '%" . addslashes($pham_brand) . "%' AND item!='$item' Order by rand() Limit 25";
mysql_query($query);

harryhermit

7:39 am on Feb 7, 2004 (gmt 0)

10+ Year Member



Timotheos,

You are the man. I don't really understand why, but what you told me works! Thanks

Timotheos

5:25 pm on Feb 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool it worked!

It would be good for you to understand why it worked.

PHP will parse any string surrounded by double quotes for variables but not functions. Therefore something like $var = addslashes($var) will work while $var = "addslashes($var)" will not work.

What I did was to concatenate the string [us4.php.net]. Follow the link and read the manual here [us4.php.net] too. It will be a great benefit for all you do. So basically what it does is tie all the strings and strings returned by funtions together into one long string. Does that make sense?

brotherhood of LAN

5:31 pm on Feb 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Also check out the mysql_escape_string() [php.net] function

jatar_k

8:21 pm on Feb 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



mysql_escape_string is the best option since it became available in 4.0.3. There is also mysql_real_escape_string [ca2.php.net] which was added in 4.3.0.

mysql_real_escape_string will escape special characters in the unescaped_string, taking into account the current charset of the connection so that it is safe to place it in a mysql_query().

harryhermit

8:44 pm on Feb 7, 2004 (gmt 0)

10+ Year Member



Thanks Tim I think I understand! Why is mysql_escape_string() a better option? Is it faster than addslashes() or is it less error prone?

jatar_k

9:04 pm on Feb 7, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It is more intuitive and built specifically for escaping strings to be used in mysql queries.