tags:

views:

66

answers:

4

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()";
+8  A: 

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!

webbiedave
+2  A: 

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()) . '\'';
Dominic Barnes
A: 

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())
);
amphetamachine
A: 
$query = "SELECT user FROM users_entity WHERE username = '".getUserName()."'";
Brant