Page 1 of 1

Warning: mysql_real_escape_string()

Posted: Thu May 01, 2008 4:43 am
by gaogier
Warning: mysql_real_escape_string() expects parameter 2 to be resource, null given in /home/gaogier/mysql_connect.php on line 11 pops up every time i try and update my database

Code: Select all

<?php
 
$dbh=mysql_connect ("localhost", "USER", "PASS") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("DATABASE");
 
function escape_data ($data){
    global $dbh;
    if (ini_get('magic_quotes_gpc')){
        $data = stripslashes($data);
    }
    return mysql_real_escape_string (trim ($data), $dbh);
}
 
?>

Re: Warning: mysql_real_escape_string()

Posted: Thu May 01, 2008 4:59 am
by kaszu
I had similar problem, add

Code: Select all

global $dbh;
at the beginning of the file.

Re: Warning: mysql_real_escape_string()

Posted: Thu May 01, 2008 5:14 am
by gaogier
do you mean the mysql connect file or the admin file (where you update your databases)?

Re: Warning: mysql_real_escape_string()

Posted: Thu May 01, 2008 6:12 am
by onion2k
Don't use global. Pass the database connection to the function as a parameter.

Re: Warning: mysql_real_escape_string()

Posted: Thu May 01, 2008 8:12 am
by gaogier
how?

Re: Warning: mysql_real_escape_string()

Posted: Thu May 01, 2008 8:52 am
by onion2k
Same way as you're passing $data.