0

I'm trying to convert usernames to their respective userid, but for some reason only the first item in the array is being converted. The rest are just nulls in the resulting array. I can enter the same name 3 times and still, only the first element of the array is converted.

Say I try to convert the following array to their userid's

$users = [1 => test1, 2 => test2, 3 => test3]

The resulting array is

$users = [1 => 1, 2 => , 3 => ]

This is the loop that looks up the userid

foreach($user_list as $user){
    array_push($user_id_list,$convert->userid_lookup($user));
}

here's the conversion function

function userid_lookup($username){
    echo $username //each name appears here
    $lookup = mysql_query("select userid from users where username = '$username' ");
    $result = mysql_fetch_array($lookup);
    echo $result['userid']; //only the first element's id appears
   return $result['userid'];
}

Why would only the first element be converted? I know each item is being processed because when I echo each item, they display.

9
  • 3
    Please, don't use mysql_* functions to write new code. They are no longer maintained and the community has begun deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide which, this article will help you. If you pick PDO, here is good tutorial. Also see Why shouldn't I use mysql functions in PHP?
    – DCoder
    Dec 27, 2012 at 19:38
  • because you are using mysql_* functions Dec 27, 2012 at 19:38
  • 1
    well, I guess, the convert function in this case returns null. so there are very few places in which this can fail. One of them being that for that username there are no records in the database. Since the function as it appears to me receives the correct parameter, i guess you can draw the conclusion yourself. If i am missing something please forgive me. But it looks like that to me.
    – Xnoise
    Dec 27, 2012 at 19:45
  • 1
    One more thing you can try to debug it: check if you have any errors reported by mysql_query. Then you can actually var_dump the result, just to check the actual type of the returned result. Also you can print the query and try to manually run it against database, see what results you get.
    – Xnoise
    Dec 27, 2012 at 19:48
  • 1
    PS: i found myself some time ago in same situation. Since then, i pretty much use trim to get rid of all the empty spaces in the beginning/end of the variables. And as a second advice, switch to mysqli, and use prepared statements.
    – Xnoise
    Dec 27, 2012 at 20:11

2 Answers 2

0

Looks to me like you have to loop through your result to the next index.

0

It seems you messed up your variable names.
setting up $users array but iterating $user_list
pushing results into $user_id_list but checking $users again.

Try to run the full code and debug it a little.

For the function itself - it looks good, though you have to properly format your string before inserting it into query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.