我有以下代码。我想用username来获取getUserName函数的值但是我正在使用语法。谁能告诉我什么是正确的?
$query = "SELECT user FROM users_entity WHERE username = getUserName()";
答案 0 :(得分:9)
您可以将句号与句点结合使用:
$query = "SELECT user FROM users_entity WHERE username = '".mysql_real_escape_string(getUserName())."'";
确保逃避您的数据!
答案 1 :(得分:2)
您无法将函数的结果直接嵌入到字符串中。但是,您可以存储变量的内容:
$username = mysql_real_escape_string(getUserName());
$query = "SELECT user FROM users_entity WHERE username = '$username'";
或者,你可以像这样连接你的字符串:
$query = 'SELECT user FROM users_entity WHERE username = \'' . mysql_real_escape_string(getUserName()) . '\'';
答案 2 :(得分:0)
您不能将(函数内部字符串替换)PHP函数名称插入到字符串中。
你可能想要更像这样的东西:
$query = sprintf("SELECT user FROM users_entity WHERE username = '%s'"
mysql_real_escape_string(getUserName())
);
答案 3 :(得分:0)
$query = "SELECT user FROM users_entity WHERE username = '".getUserName()."'";