Reputation: 371
I have an SQL query to check status in table. This is my table and record:
#table product#
id_pd id_po status
1 po001 0
2 po001 0
3 po001 0
4 po001 1
echo "done";
if all of record have status 1
by id_po ?
<?php
include 'connection.php';
$qry = $db->query("SELECT status FROM product WHERE id_po='$id_po' ");
$fet = $qry->fetch_assoc();
if($fet['status'] == 1){
echo "done";
}
?>
Upvotes: 0
Views: 395
Reputation: 2044
Your condition is if all status is 1 then status will be ok. In this situation, you may try this
// get all
$qry = $db->query("SELECT count(*) all_status FROM product WHERE id_po='$id_po'");
$fet = $qry->fetch_assoc();
// get active
$qry2 = $db->query("SELECT count(*) all_status FROM product WHERE id_po='$id_po' AND status >= 1");
$fet2 = $qry2->fetch_assoc();
// match both record
if($fet1['all_status'] == $fet2['all_status']){
echo "done";
}
Upvotes: 0
Reputation: 90746
You could check by min status:
$qry = $db->query("SELECT min(status) as min_status FROM product WHERE id_po='$id_po' group by id_po");
$fet = $qry->fetch_assoc();
if($fet['min_status'] == 1){
echo "done";
}
Upvotes: 3
Reputation: 127
try this, i have not try it, but I think it give you idea))
<?php
include 'connection.php';
$status = true;
$qry = $db->query("SELECT status FROM product WHERE id_po='$id_po' ");
while ($fet = mysql_fetch_assoc($qry)) {
if($fet['status'] == 0){
$status = false;
}
}
if($status){
echo 'done';
}
?>
Upvotes: 0