MYSQL-PHP获取特定单元格(字段)的值,并在PHP IF语句中使用它

时间:2022-10-17 13:35:13

im hoping anyone can help with this issue. Heads up im in the process of learning both PHP and MySQL so please bear with me. I have the following table:

我希望任何人都能帮助解决这个问题。在学习PHP和MySQL的过程中请注意im,请耐心听我说。我有以下表格:

ID  whid    securitybatch   radionumber userbatch    status       dateofentry
1   AAA        123456           1        5555555    Available   11/10/2016 9:20
2   BBB        123456           7        1234567    Available   11/10/2016 16:50
3   BBB        123456           6        6666666    Deployed    11/11/2016 7:43
4   XXX        123456           3        2222222    Deployed    11/11/2016 19:52
5   XXX        123456           4        1234567    Deployed    11/11/2016 20:03
6   AAA        123456           6        1111116    Deployed    11/11/2016 21:43
7   XXX        123456           2        2222222    Deployed    11/11/2016 21:52

Im trying the get the highlighted value and use it in an IF statement, the issue is my specific requirements.

我正在尝试获取突出显示的值并在IF语句中使用它,问题是我的特定需求。

The user 1234567 has two entries on the table and eventually a lot more, i want to get the value of the status column of the latest dated entry for that user, being the one that is selected, if that makes sense...ive been trying and researching but its beating me up...

用户1234567在表中有两个条目,最终会有更多,我想为该用户获取最新日期条目的状态列的值,作为被选中的那个,如果有意义的话……我一直在努力研究,但它打败了我……

$query = "SELECT status FROM radiodata WHERE userbatch='$user' AND    MAX(dateofentry)";
$result = mysqli_query($dbc, $query);     

So i would love to get the value 'Deployed' a string i suppose and use it as follows:

因此,我想让这个值“部署”一个字符串,并将其用于如下所示:

if($result === 'Deployed') {
    echo 'Not returned';
} else {
    echo 'Returned';
}

Thanks in advance, any ideas/solutions are welcome.

预先感谢,任何想法/解决方案都是受欢迎的。

Cheers

干杯

3 个解决方案

#1


1  

That query in it's current form is invalid and will lead to a syntax error.

该查询的当前形式无效,将导致语法错误。

$query = "SELECT status FROM radiodata WHERE userbatch='$user' ORDER BY dateofentry DESC LIMIT 1";
$result = mysqli_query($dbc, $query);     

You really shouldn't be using string concatenation like this, but let's leave that aside for now. This query will now return you a single row result.

你真的不应该像这样使用字符串连接,但是我们先把它放在一边。该查询现在将返回单个行结果。

Having done that you still can't compare it to deployed in that manner

完成了之后,您仍然无法将其与部署的方式进行比较。

$row = mysqli_fetch_array($result);
if ($row['status'] == 'Deployed') {

}

#2


1  

You can order the query by your dateofentry

您可以通过dateofentry对查询进行排序

just add

只需添加

ORDER BY dateofentry DESC LIMIT 1

按进口日期订购DESC限额1

to the end of your query and it'll sort it by that column and then the LIMIT will only get you the one result

在查询的末尾,它会按列进行排序,然后这个限制只会得到一个结果

#3


1  

You can do as follows:

你可以做如下:

$query = "SELECT status FROM radiodata WHERE userbatch='$user' ORDER BY 
          dateofentry  DESC LIMIT 1";
$result = mysqli_query($dbc, $query); 

$row = mysqli_fetch_row($result);  

if($row[0] == 'Deployed') {
    echo 'Not returned';
} else {
    echo 'Returned';
}

#1


1  

That query in it's current form is invalid and will lead to a syntax error.

该查询的当前形式无效,将导致语法错误。

$query = "SELECT status FROM radiodata WHERE userbatch='$user' ORDER BY dateofentry DESC LIMIT 1";
$result = mysqli_query($dbc, $query);     

You really shouldn't be using string concatenation like this, but let's leave that aside for now. This query will now return you a single row result.

你真的不应该像这样使用字符串连接,但是我们先把它放在一边。该查询现在将返回单个行结果。

Having done that you still can't compare it to deployed in that manner

完成了之后,您仍然无法将其与部署的方式进行比较。

$row = mysqli_fetch_array($result);
if ($row['status'] == 'Deployed') {

}

#2


1  

You can order the query by your dateofentry

您可以通过dateofentry对查询进行排序

just add

只需添加

ORDER BY dateofentry DESC LIMIT 1

按进口日期订购DESC限额1

to the end of your query and it'll sort it by that column and then the LIMIT will only get you the one result

在查询的末尾,它会按列进行排序,然后这个限制只会得到一个结果

#3


1  

You can do as follows:

你可以做如下:

$query = "SELECT status FROM radiodata WHERE userbatch='$user' ORDER BY 
          dateofentry  DESC LIMIT 1";
$result = mysqli_query($dbc, $query); 

$row = mysqli_fetch_row($result);  

if($row[0] == 'Deployed') {
    echo 'Not returned';
} else {
    echo 'Returned';
}