Warning: 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
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Warning: mysql_real_escape_string()

Post 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);
}
 
?>
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Warning: mysql_real_escape_string()

Post by kaszu »

I had similar problem, add

Code: Select all

global $dbh;
at the beginning of the file.
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: Warning: mysql_real_escape_string()

Post by gaogier »

do you mean the mysql connect file or the admin file (where you update your databases)?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Warning: mysql_real_escape_string()

Post by onion2k »

Don't use global. Pass the database connection to the function as a parameter.
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: Warning: mysql_real_escape_string()

Post by gaogier »

how?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Warning: mysql_real_escape_string()

Post by onion2k »

Same way as you're passing $data.
Post Reply