mysql_real_escape_string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

mysql_real_escape_string

Post by dejvos »

Hi,

I've problem with mysql_real_escape_string() function. It doesn't return expected value.

Code: Select all

 
$where=array('id' => 4);
foreach($where as $col => $val){
    if(!empty($sWhere)) $sWhere .= ",";
    $sWhere .= mysql_real_escape_string($col).'="'.mysql_real_escape_string($val).'"';
}
//returns ="" string; It should be id="4", shoudn't it?
 
Any suggestion how is this possible?

Thanks.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: mysql_real_escape_string

Post by daedalus__ »

uh im not sure about how scope works in php because im very rusty but if you define sWhere inside of a foreach wouldn't it only exist in the scope of that loop?
:\

and yeah i think it should be four unless mysql_escape_string() around the 4 is failing for some reason.

error check :|

or statements exception get_defined_vars(), etc.

assign a variable as mysql_real_escape_string($val); then var_dump it, etc.
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: mysql_real_escape_string

Post by dejvos »

I've probably solve it. The problem was that there wasn't mysql connection opened and mysql_real_escape_string needs to have mysql_connection opened.

Regard!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: mysql_real_escape_string

Post by jackpf »

daedalus__ wrote:uh im not sure about how scope works in php because im very rusty but if you define sWhere inside of a foreach wouldn't it only exist in the scope of that loop?
:\
I don't think so. Loops are in the global scope.
Post Reply