1

I'm trying to understand whether mysql_real_escape_string($str) can be empty if $str is not empty (by $str is empty I mean that empty($str) is true)?

5
  • 6
    mysql_real_escape_string will be empty if you are not connected to a database. Commented Jul 16, 2012 at 15:56
  • 6
    Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Jul 16, 2012 at 15:57
  • 1
    @Rocket No it will not return empty string, instead it will be FALSE of boolean type Commented Jul 16, 2012 at 15:58
  • 1
    @AlvinWong: If he was trying to use it as a string, the FALSE would be converted to a string. echo FALSE will echo nothing. Commented Jul 16, 2012 at 15:58
  • @Alvin Depends on how you define "empty". If you define it with the empty() function, i.e. == false, then both an "empty string" and false are "empty". Commented Jul 16, 2012 at 16:13

1 Answer 1

2

mysql_real_escape_string

Returns the escaped string, or FALSE on error.

So the return value is either false if there's an error (like not being connected to the database), which counts as "empty". Or it's the escaped string, which means that there are additional escape characters in the string. mysql_real_escape_string will not reduce the string.

So if the string has content and you are connected to the database, if the input is not "empty", the output won't be either. If there's an error though, the output will be "empty".

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.