Page 1 of 1

mysql_real_escape_string

Posted: Fri Dec 04, 2009 5:07 am
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.

Re: mysql_real_escape_string

Posted: Fri Dec 04, 2009 6:19 am
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.

Re: mysql_real_escape_string

Posted: Fri Dec 04, 2009 6:38 am
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!

Re: mysql_real_escape_string

Posted: Fri Dec 04, 2009 6:51 am
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.