[英]MYSQL-PHP Get value of a specific cell(field) and use it in PHP IF statement
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.
在学习PHP和MySQL的过程中向我迈进,所以请耐心等待。 I have the following table:
我有下表:
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: 所以我很乐意将'Deployed'这个值设为我认为的字符串并使用如下:
if($result === 'Deployed') {
echo 'Not returned';
} else {
echo 'Returned';
}
Thanks in advance, any ideas/solutions are welcome. 在此先感谢,欢迎任何想法/解决方案。
Cheers 干杯
You can order the query by your dateofentry 您可以通过dateofentry订购查询
just add 加上
ORDER BY dateofentry DESC LIMIT 1
ORDER BY dateofentry DESC LIMIT 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 在查询结束时,它将按该列对其进行排序,然后LIMIT只会获得一个结果
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') {
}
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';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.