I found out that moodle doesn't recognize usernames without apostrophes. Doing this query is the right way to go:
$usernames = array('username1','username2',...);
list($usernamewhere, $usernameparams)= $DB->get_in_or_equal($usernames, SQL_PARAMS_NAMED,'username');
$sql ="SELECT u.id AS userid, u.username
FROM {user} u
WHERE u.username {$usernamewhere}";
$users = $DB->get_records_sql($sql, $usernameparams);
foreach($users as $user){
echo $user->userid .' - '. $user->username .'<br />';}
But since my resulting array can have different values/size, it's not pratical at all. So, before the query, I have this
function apply_quotes($item){return"'".mysql_real_escape_string($item)."'";}
$dataClip=array_map("apply_quotes",$dataClip);
$allUsers=array_map("apply_quotes",$allUsers);
$results=array_intersect((array)$dataClip,(array)$allUsers);
Changing my query to this, gives me no output. Can anyone help me?
list($usernamewhere, $usernameparams)= $DB->get_in_or_equal($results, SQL_PARAMS_NAMED,'username');
$sql ="SELECT u.id AS userid, u.username
FROM {user} u
WHERE u.username {$usernamewhere}";
$users = $DB->get_records_sql($sql, $usernameparams);
foreach($users as $user){
echo $user->userid .' - '. $user->username .'<br />';}