Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,341
    Thanks
    260
    Thanked 32 Times in 31 Posts

    changing function get no db connect error

    im updating some php files that have the old mysql_escape_string function to the newer mysql_real_escape_string function and for some reason when i change it, it gives me a no database connection error, but yet its fine with the old function name, thats wierd the db connection is there why would it matter if i change the function name to the newer one.. i guess im alittle lost on this one.. thanks

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Sample of your code?

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,341
    Thanks
    260
    Thanked 32 Times in 31 Posts
    sorry about that..

    Code:
    $userid = clean($_GET['id']);
    and clean is

    Code:
    function clean($value){
    	return mysql_escape_string(trim(strip_tags($value)));
    }
    but when i change it to mysql_real_escape_string i get an db error..

    also in a normal line of code i have several of these

    Code:
    $name =  mysql_escape_string($name);
    and when i change it to

    Code:
    $name =  mysql_real_escape_string($name);
    i also get a db connect error..

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Are you sure that you have connected to the DB before you use the escape function? If I recall correctly, mysql_escape_string worked whether a connection was active or not, whereas real_escape requires that the DB connection be active first. I could be wrong, but I seem to vaguely recall something along those lines from wayback on here.

  • Users who have thanked MattF for this post:

    durangod (11-22-2010)

  • #5
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,341
    Thanks
    260
    Thanked 32 Times in 31 Posts
    ahhhhhhhhhh ill look into that, might be just the thing, those sneaky guys new function and new rules lol too funny... thanks

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That is likely your issue. MySQL_escape_string was simply a fancy addslashes for mysql. It was PHP created. Real_escape_string on the other hand uses the native MySQL functions to correct this issue.
    If you are in the works of updating, I suggest looking into the usage of either MySQLi classes or PDO for your connectivity. These both provide you with the awesome prepared statements syntax which virtually eliminates the use of real_escape_string functions.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    durangod (11-22-2010)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •