www.webdeveloper.com
Results 1 to 13 of 13

Thread: how do you make user id to username

  1. #1
    Join Date
    Oct 2006
    Posts
    203

    how do you make user id to username

    I have a db that when i get some results i get the user_id like this:

    Code:
    <?php echo $report_row['user_id']; ?>
    How can i change this to username - i have this in the form already:

    Code:
    <?php
    $user_result = mysql_query("SELECT * FROM users ORDER BY username", $db);
    while($user_row = mysql_fetch_array($user_result))
    {
    ?>
    <option value="<?php echo $user_row['user_id']; ?>"><?php echo $user_row['username']; ?></option>
    I tried username on the report_row but looks like im missing something..

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    What is the column name in the database for the user name in that table? That will be your array key (case-sensitive).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2006
    Posts
    203
    Quote Originally Posted by NogDog View Post
    What is the column name in the database for the user name in that table? That will be your array key (case-sensitive).
    well its 'users' on the 'main' db

    and in users theres:
    Field:

    user_id
    username [SELECT COUNT( * ) AS `Rows` , `username` ]
    first_name
    last_name
    Last edited by riskmod; 02-15-2011 at 11:42 AM.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    Unless there is some syntax error I'm not seeing, the code you posted originally looks OK to me. The only problem I could see was if "username" was not the correct column name. If it is, then you code ought to work OK, I think. You could do a quick check by changing it to this just for debugging:
    PHP Code:
    <?php
    $user_result 
    mysql_query("SELECT * FROM users ORDER BY username"$db);
    while(
    $user_row mysql_fetch_array($user_result))
    {
       
    // delete or comment out after done debuggin:
       
    echo "<pre>".print_r($user_rowtrue)."</pre>";
       exit;
       
    // end debug code
    ?>
    <!-- rest of code -->
    That will show you exactly what data was retrieved by your query and what the array keys are.

    PS: Just a little "style" note: if you are only going to reference the row results by column name, I'd suggest using mysql_fetch_assoc() instead of mysql_fetch_array(), as the latter stores each column twice: once with the column name key and once with a numeric index in the array.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Oct 2006
    Posts
    203
    Quote Originally Posted by NogDog View Post
    Unless there is some syntax error I'm not seeing, the code you posted originally looks OK to me.
    NogDog thanks for the mysql_fetch_assoc() tip.

    Ok checked good but I still cant get it as a result like this:

    Code:
    				while($report_row = mysql_fetch_array($report_result))
    				{
    					?>
    <tr>
    <td <?php if(($count &#37; 2) == 0) echo 'class="alt_row"'; ?>><?php echo getStatus($report_row['status'], $db); ?></td>
    <td <?php if(($count % 2) == 0) echo 'class="alt_row"'; ?>><?php echo date("n/d/y g:i a", strtotime($report_row['created'])); ?></td>
    <td <?php if(($count % 2) == 0) echo 'class="alt_row"'; ?><?php echo $report_row['user_id']; ?></td>
    Again it works with user_id but I tried username from users in the row but still haven't had luck

    Here's another way i can get it but not in the results:

    Code:
    <?php
    if(!empty($user))
    {
    ?>								
    <b>User:</b>
    <?php				
    array_walk($user, "arrayChangeUser", $db);
    echo implode(", ", $user);?><br />
    <?php
    }
    ?>
    Last edited by riskmod; 02-15-2011 at 01:09 PM.

  6. #6
    Join Date
    Oct 2006
    Posts
    203
    Quote Originally Posted by NogDog View Post
    PHP Code:
    <?php
    $user_result 
    mysql_query("SELECT * FROM users ORDER BY username"$db);
    while(
    $user_row mysql_fetch_array($user_result))
    {
       
    // delete or comment out after done debuggin:
       
    echo "<pre>".print_r($user_rowtrue)."</pre>";
       exit;
       
    // end debug code
    ?>
    <!-- rest of code -->
    That will show you exactly what data was retrieved by your query and what the array keys are.
    Oh forgot when I did that it hid/erased the usernames in the list fyi.

    I tried the user_rows just still not implementing it correctly I guess

  7. #7
    Join Date
    Oct 2006
    Posts
    203
    Anyone else have any ideas?

    I just want to know what to input here:

    <?php echo $report_row['user_id']; ?>

    how can I change this to username ('username' will define nothing)

    Am I just missing an edit on the result/report row?:

    while($report_row = mysql_fetch_array($report_result))

  8. #8
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    It seems that you must not know the correct names of the columns in your table. Apparently `username` is not one of the column names, maybe it is user_name, or usernames or last_name, first_name which could be combined in mysql with something like;

    PHP Code:
    $query "SELECT CONCAT(first_name, " ", last_name) AS username FROM users ORDER BY username ASC"
    If I am having a query problem I often sort out the problem using phpMyAdmin and doing queries there before committing them to php code.
    Last edited by Dasher; 02-18-2011 at 01:00 PM.

  9. #9
    Join Date
    Oct 2006
    Posts
    203

    thanks

    Thanks Dasher yeah I'll look into that but how was this define then?

    Code:
    <select>
    <?php
    $user_result = mysql_query("SELECT * FROM users ORDER BY username", $db);
    while($user_row = mysql_fetch_array($user_result))
    {
    ?>
    <option value="<?php echo $user_row['user_id']; ?>"><?php echo $user_row['username']; ?></option>.......
    It shows the username there as a select box...I can select the user and then get the results....RESULTS is where i want a column with the USERNAME there...so I added another column in the result where it showed from what user it was made from which it does work:

    <td <?php if(($count &#37; 2) == 0) echo 'class="alt_row"'; ?><?php echo $report_row['user_id']; ?></td>

    I just need this to be username. Obviously it doesn't make sense if i looked it under the username, but it will make sense if i do a report base on the 'date' and so on....

    I need to define the $user_row for the report_row..

    Thanks

  10. #10
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Does this work? I don't like to constantly switch back and forth between html and the php engine unless I have to so I usually echo a lot of html;

    PHP Code:
    $query "SELECT userid, username FROM users ORDER BY username ASC";

    $result mysql_query($query,$link);
    $count=0;
    echo 
    "<table><tr><td>User ID</td><td> Username </td></tr>";
    while (
    $report_row mysql_fetch_assoc($result)){

    $userID $report_row['userid'];
    $userName $report_row['username'];

    if (
    $count == 0) {
    echo 
    "<tr><td class= 'alt_row'>" $userID "</td><td class= 'alt_row'>" $userName "</td></tr>";
    }else {echo 
    "<td>" $userID "</td><td>" $userName "</td></tr>";}
    $count++;
    }
    echo 
    "</table>"
    Last edited by Dasher; 02-18-2011 at 06:07 PM.

  11. #11
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Opps I left out a <tr> in the } else { echo "<tr><td>... line.

  12. #12
    Join Date
    Oct 2006
    Posts
    203
    Quote Originally Posted by Dasher View Post
    Opps I left out a <tr> in the } else { echo "<tr><td>... line.

    I tried that but it gave me nothing...Is there a way i can show the whole code here? Anyone else have any ideas?

  13. #13
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Change this line;

    PHP Code:
    $result mysql_query($query,$link); 
    to this

    PHP Code:
    $result mysql_query($query); 
    or better to this;

    PHP Code:
    $result mysql_query($query)or die("<b>A fatal MySQL error occurred</b>.\n<br />Query: " $query "<br /><br />\nError: (" mysql_errno() . ") " mysql_error()); 
    Have you tried to execute the MySQL query using phpMyAdmin?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles