使用PHP获取MYSQL数据库表中的最后一项

时间:2022-04-17 07:57:09

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).

我的数据库中有一个表,列有entry_id(INT- Auto_Increment)、user_id(INT)、firstname(VARCHAR)、lastname(VARCHAR)和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".

我的脚本基本上是我尝试创建的一个非常基本的时间表,当用户访问脚本时,将设置user_id、firstname和lastnames。所以我需要做的是检查最后一个用户的评论单元格,看看这个评论是“in”还是“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.

我连接到数据库并获得一个简单的查询。userid会话已经设置好。在问这个问题之前,我查看了这里和这里的答案。

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

这是$row打印的内容,注意注释是如何为空的,尽管我知道我在数据库中有一个值。

Array
(
    [entry_id] => 4
    [user_id] => 3
    [comment] => 
)

1 个解决方案

#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.

您应该已经看到了那个未定义的索引注释。

#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.

您应该已经看到了那个未定义的索引注释。