Forum Moderators: coopster

Message Too Old, No Replies

returning results with an '

         

dkin

10:46 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



I am using this script

$mresult = mysql_query("SELECT DISTINCT game FROM vids order by id desc limit 50", $link) or die ("query 1: " . mysql_error());

while ($mrow = mysql_fetch_array($mresult))
{
$count = mysql_result(mysql_query("SELECT COUNT(*) FROM vids where game = '$mrow[game]'"), 0);
}

but for any $mrow[game] that has an ' in it a 0 is returned, how do I fix this?

ergophobe

11:00 pm on Jul 27, 2005 (gmt 0)

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



I'm not sure how you got the data into the DB, but the quote marks need to be escaped in the data and then the slashes need to stripped when you pull the data out of the DB.

A raw ' in the data is going to create problems.

StupidScript

11:10 pm on Jul 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ergophobe is entirely correct ... however, you can get out of the hole you dug ... ;)

while ($mrow = mysql_fetch_array($mresult))

{

[b]$thisTitle=addslashes($mrow["title"]);[/b]

$count = mysql_result(mysql_query("SELECT COUNT(*) FROM vids where game = [b]'$thisTitle'[/b]"), 0);

}

Note the use of quotes instead of apostrophes in the addslashes() function. This escapes the title's apostrophes (like

O\'Reilly
)(but use with caution for other things ... see the manual page [us3.php.net]) and uses that escaped string as the $thisTitle variable's value. Then, when using that data in your SELECT statement, the escaped apostrophes do not interfere with the apostrophes you are using in your SELECT statement, and the value is passed as-is (no escaping) for comparison with the values in the database.

In the future you may want to consider using addslashes() or another special-character-modifying function before you dump the data into the db.

jatar_k

11:15 pm on Jul 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> I'm not sure how you got the data into the DB

hopefully this way
[webmasterworld.com...]

;)

ergophobe

11:54 pm on Jul 27, 2005 (gmt 0)

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



heh heh... but then the single quotes would be escaped with a slash.

<snip>

jatar_k

1:59 am on Jul 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



true but thought I would bring it up ;)

be careful with those snippers, I don't want you bleeding all over the forum

ergophobe

3:53 pm on Jul 28, 2005 (gmt 0)

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



Hey man. I know the risks I take. My left ear used to be bigger back when I still had professionals cut my hair.

I'm just trying to remember when I've ended up with unescaped quotes in my DB. Double escaped quotes through the magic of magic_quotes_gpc, yes, which ends up being being unescaped in the long run as the slash gets escaped.