1

I know this question has been asked multiple times on here but each one is a little different.

I have a table in my database with the columns entry_id(INT- Auto_Increment), user_id(INT), firstname(VARCHAR), lastname(VARCHAR) and comment(VARCHAR).

My script is pretty much a very basic timesheet I have tried creating, the user_id, firstname and lastnames are set when the user visits the script. So what I need to do is check the last comment cell for that user to see if the comment is either "in" or "out".

Whenever I try building my query and checking if the last comment in that users field is either in or out, I visit my script and it doesn't print the comment, what am I doing wrong?

$userid = $_SESSION['user_id'];

    $query = mysqli_query($mysqli, "SELECT entry_id, user_id FROM timesheet WHERE user_id = '$userid' ORDER BY entry_id DESC LIMIT 1") or die('Error: ' . mysqli_error($mysqli));

    if(mysqli_num_rows($query)>0){

     while ($row = mysqli_fetch_array($query, MYSQL_ASSOC)) {
        echo '<pre>';
        print_r($row);
        echo '</pre>';

        $clock = $row['comment'];

        echo $clock . ' Last entry';

    } 

Couple of notes:

I AM connected to the database and get a simple query to work. The userid session is set. Prior to asking this question I looked at the answers here and here.

Here is what $row is printing, notice how comment is empty although I know I have a value in the database.

Array
(
    [entry_id] => 4
    [user_id] => 3
    [comment] => 
)
4
  • Does the query provide the right result? Commented Sep 14, 2014 at 0:41
  • 2
    You're querying for entry_id and user_id but when you try to access the results, you're referencing comment as in $row['comment']. Where is that supposed to come from? The only keys in $row will be the fields you queried. Commented Sep 14, 2014 at 0:41
  • 2
    tip: always turn on error reporting! error_reporting(E_ALL); ini_set('display_errors', '1'); Commented Sep 14, 2014 at 0:43
  • Thanks Ghost, epic fail on that, turns out the include path was incorrect, cheers! Commented Sep 14, 2014 at 0:46

1 Answer 1

2

Just as you seen on comments, you trying to access an index which is not included with the one you queried that's why you dont get any results.

$userid = $_SESSION['user_id'];
$query = mysqli_query($mysqli, 
    "SELECT 
        entry_id, 
        user_id,

        comment <-- you're trying to access comments, but didn't include that column in the query

        FROM timesheet 

        WHERE user_id = '$userid' ORDER BY entry_id DESC LIMIT 1") 

or die('Error: ' . mysqli_error($mysqli));

if(mysqli_num_rows($query) > 0){

    while ($row = mysqli_fetch_assoc($query)) {

        echo $row['comment'] . ' Last entry <br/>';
    }
}

If you've had turn on error reporting:

error_reporting(E_ALL); 
ini_set('display_errors', '1');

You should have seen that undefined index comment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.