Select and display specific item from an array

I have this function that gets the last user login time;

/////////////////////////////////////////////////////////////////////////////////////////////////////// 
// BEGIN QUERY TO GET USER DATA
    function get_userlastlogin($uid,$un) {
        require 'includes/db.config.php';

    $query = "SELECT 
                users.user_lastlogin
            FROM users
            WHERE users.id = '$uid' AND username = '$un'
        "; 
    try 
    { 
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        die("Failed to run query: " . $ex->getMessage()); 
    } 
    $users_data = $stmt->fetchAll(PDO::FETCH_ASSOC); 
    return $users_data;
}

I want to use it with this code to return just the lastlogin value;

<?php
          $time = strtotime(get_userlastlogin($_SESSION['user']['id'],$_SESSION['user']['username']));
          $one_week_ago = strtotime('-1 week');

          if( $time > $one_week_ago ) { 
            echo "<span class='pull-right' style='color:red;'>NEW</span>";
          }
        ?>

At present Iā€™m getting this eror;

NOTICE: UNDEFINED INDEX: USER_LASTLOGIN IN C:\XAMPP\HTDOCS\LOCAL-X\INCLUDES\QUERIES.PHP ON LINE 854

And when I print the function, it is returning the entire array;

you have a 2D array, not a 1D array.

1 Like

None of this really has any relevance to the error. Undefined Index usually means that you are referencing a variable that was never created and thefore would make no sense. I assume you are trying to call your function.

Also, since you are using prepared statements, please use them correctly. What you are doing IS the cause of SQL Injection.

1 Like

Itā€™s weird that your query only retrieves one single column, yet the array returned contains more than one column, almost as if your query was running ā€˜select *ā€™ rather than as you have it. Any chance youā€™ve got an old version on the server that is using the wrong query?

On another thing, I think it would be clearer if you just used fetch() instead of fetchall() - the nature of the query is such that it should only return one row, youā€™re assuming it only returns one row in any case, so you could remove the second dimension of the array (by using fetch()) and make things nicer. IMO of course.

Sorry, I managed to sort out the function with some help, here is the final code Iā€™m using;

/////////////////////////////////////////////////////////////////////////////////////////////////////// 
// BEGIN QUERY TO GET USER DATA
    function get_specificuserinfo($uid,$un,$col) {
        require 'includes/db.config.php';

    $query = "select " . $col . " from users where users.id = '$uid' and username = '$un'";
    try 
    { 
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    $data = $stmt->fetchColumn(); // you could use this as it's only a single column
    return $data; // after checking it returned something

}

My issue now is I want to use a similar method to get profile data, but itā€™s more complex because I have numerical values stored in the profiles table (say for height for example) that link to another tableā€™s id frmHeight for example that should then return the actual human friendly value ā€˜5ft 8"ā€™ for example.

Iā€™m using joins the query to display the full profile but am in need of calling the specific values for the edit profile pages.

You need to either have a function that ā€œknowsā€ that some columns require a separate lookup and have it cope with that, or call the profile function to get the height-code and then, in your calling code, retrieve the description for the height. Because the profile seems to have joins to other tables, then perhaps thatā€™s a reason for it reverting to your original code style that loads the entire profile and only returns the required column.

Might have been better to store the height in inches and just have a display function to convert it into feet and inches, rather than a look-up table. Apart from anything else, itā€™d be easier to sort or perform calculations on it.

2 Likes

With height in feet and inches, I would probably store as numerical data, either decimal feet, or inches as an integer, as thatā€™s more machine friendly. Then have a function to convert the value to a human friendly string on retrieval.

Edit Ah, just saw @droopsnoot has said that.

1 Like

Thanks Droopsnoot.

Decided to go with CM using the following code;

function cm2feet($cm)
{
     $inches = $cm/2.54;
     $feet = intval($inches/12);
     $inches = $inches%12;
     return sprintf('%d ft %d ins', $feet, $inches);
}

Thanks for your help @droopsnoot and @SamA74 :thumbsup:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.