I have the following code. I would like username to take the value of the getUserName function however I am fighting with syntax. Can anybody tell me what should be the correct one?
$query = "SELECT user FROM users_entity WHERE username = getUserName()";
asked Apr 19, 2010 at 15:57
Vonder
4,06115 gold badges46 silver badges63 bronze badges
4 Answers 4
You can use concatenation with the period:
$query = "SELECT user FROM users_entity WHERE username = '".mysql_real_escape_string(getUserName())."'";
Make sure to escape your data!
answered Apr 19, 2010 at 15:59
webbiedave
49k8 gold badges92 silver badges104 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can't embed the result of a function directly into a string. However you can store the contents of a variable:
$username = mysql_real_escape_string(getUserName());
$query = "SELECT user FROM users_entity WHERE username = '$username'";
Or, you could concatenate your string like this:
$query = 'SELECT user FROM users_entity WHERE username = \'' . mysql_real_escape_string(getUserName()) . '\'';
answered Apr 19, 2010 at 15:59
Dominic Barnes
28.5k8 gold badges69 silver badges91 bronze badges
Comments
You cannot interpolate (internally string-replace) PHP function names into strings.
You probably want something more like this:
$query = sprintf("SELECT user FROM users_entity WHERE username = '%s'"
mysql_real_escape_string(getUserName())
);
answered Apr 19, 2010 at 16:00
amphetamachine
31k12 gold badges69 silver badges74 bronze badges
Comments
$query = "SELECT user FROM users_entity WHERE username = '".getUserName()."'";
answered Apr 19, 2010 at 16:00
Brant Messenger
1,45111 silver badges22 bronze badges
Comments
default