odd mysql_real_escape_string behavior

Discussion in 'PHP' started by c71123c, Jan 27, 2008.

  1. #1
    I've been trying to figure out what I am not doing correctly when using the mysql_real_escape_string function. I set up a test file that only contains (config.php are my mysql settings):

    <?php
    	include('config.php');
    	
    	echo(mysql_real_escape_string("The next line won't work"));
    
    	$fixed = mysql_real_escape_string("Now it's working!");
    	echo($fixed);
    ?>
    Code (markup):
    and the resulting output is:

    Second line doesn't show up... Any time I try to use mysql_real_escape_string within a variable, it breaks the code. Any ideas what's happening?
    -- JK
     
    c71123c, Jan 27, 2008 IP
  2. InFloW

    InFloW Peon

    Messages:
    1,488
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Have you made an actual mysql connection? mysql_real_escape_string requires you are connected in order to use it.

    It's strange that the first one works and the second will not and that's the only thing I can think of at this moment that may cause this.
     
    InFloW, Jan 27, 2008 IP
  3. c71123c

    c71123c Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, the connection is established in the 'config.php' file and I am able to make queries to the database.
     
    c71123c, Jan 27, 2008 IP
  4. InFloW

    InFloW Peon

    Messages:
    1,488
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #4
    So if you do something like

    
    <?php
    	include('config.php');
    	
    	$fixed = "I AM NOT FIXED";
    
    	$fixed = mysql_real_escape_string("Now it's working!");
    	echo $fixed;
    ?>
    
    PHP:
    You get what values back exactly when you echo fixed? I'm going to assume you get nothing so do var_dump($fixed); and see what it gives you for the type of $fixed. I assume it will be a boolean and be FALSE.
     
    InFloW, Jan 27, 2008 IP
  5. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #5
    What's your error reporting set to?

    Add
    
    error_reporting(E_ALL);
    echo(mysql_real_escape_string("The next line won't work"));
    
    PHP:
    to the top of your file (yes, before config.php). Perhaps PHP is not outputting errors.

    The only reason I have said this, is because the 2nd line should thow an error saying there is no MySQL resource link. If it does, remove it and refresh, and see if any more errors are outputted. If there is no error, PHP.ini is set not to display_errors (try using ini_set()).

    Jay
     
    jayshah, Jan 27, 2008 IP
  6. c71123c

    c71123c Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for the help on this issue. I have found the problem, which is of course the stupidest user error. There were invisible non-breaking spaces on both sides of the '=' following the variable. I had to show invisibles in order to see that there was something funny going on. Probably from copying and pasting in the process. Switched to regular spaces and all is right with the world. I knew it was something ridiculously stupid like that.
    -- peon
     
    c71123c, Jan 27, 2008 IP