mysql count where row = 0 - php

I am trying to show a count but only where the column deleted is equal to 0
here is what i have tried
$result=mysql_query("SELECT * FROM users where deleted=0")or die('Error ' );
$counter = mysql_query("SELECT COUNT(*) as personID FROM users");
$row = mysql_fetch_array($result);
$personID = $row['personID'];
$personFname = $row['personFname'];
$personSname = $row['personSname'] ;
$llmail = $row['llmail'];
$mainadmin = $row['mainadmin'];
$delete = $row['delete'];
$num = mysql_fetch_array($counter);
$count1 = $num["personID"];
This shows a count of 4 however of the 4, 2 are deleted so it should only show 2 if this makes sense?

SELECT COUNT(*) as personID FROM users WHERE deleted=0

Related

php mysql query is not doing sum properly

I am doing sum with the below query but it is not giving result properly.
If there are four items and it is showing the result like:
1.000 2.000 3.000 4.000 and it should be like 10.000
I don't know where I am mistaken please help.
<?php
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTC = $torder['item_id'];
$qtyT = $torder['qty'];
$chTP = mysql_query("
select * from temp_choices
where item_id = '".$prITTC."'
AND ses_mem = 113
AND status = 1
");
while($chGET = mysql_fetch_array($chTP)){
$fID = $chGET['id'];
$field = $chGET['choice_id'];
$order_tempCHP = mysql_query("
select sum(price) as total, id, ename, choice_id, item_id, price
from choice_price
WHERE
id IN('$field')
");
while ($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }
}
?>
according to my question above after trying and trying i found the solution and resolved my problem according to below code:
Thanks to #John Kugelman at MySQL query using an array
$order_temp = mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."' and ses_mem=113 order by id");
while ($torder = mysql_fetch_array($order_temp)) {
$prITTD = $torder['id'];
$prITTC = $torder['item_id'];
$chTPaa = mysql_query("
select choice_id
FROM temp_choices
WHERE item_id = '$prITTC'
AND ses_mem = 113
AND status = 1
group by choice_id
");
while ($chGETaa = mysql_fetch_assoc($chTPaa)){
$temp[] = $chGETaa['choice_id'];
}
$thelist = implode(",",$temp);
$order_tempCHP = mysql_query("
select sum(price) as total
from choice_price
WHERE
id IN ($thelist)
AND
item_id = '".$prITTC."'
");
while($torderCP = mysql_fetch_assoc($order_tempCHP)){
$totalCH = $torderCP['total'];
$tsl = $totalCH+($qtyT*$prIDTC);
$altsl = number_format($tsl, 3, '.', '');
echo $altsl;
} }

how to execute multiple count query in php

I executed 4 count queries from one table. but I am getting the same output from all the queries. but the actual value is different in the table.
Here is my table.
ID || notify_type || status
__________________________________________
1 || resume_uploaded || 1
Here are my queries:
$notify_query1 = "select count(*) from notify where status = 1 and notify_type = 'resume_uploaded'";
$row1 = mysqli_query($db_manager->connection,$notify_query1);
$rcount = mysqli_num_rows($row1);
$notify_query2 = "select count(*) from notify where status = 1 and notify_type = 'detail_filled'";
$row2 = mysqli_query($db_manager->connection,$notify_query2);
$dcount = mysqli_num_rows($row2);
$notify_query3 = "select count(*) from notify where status = 1 and notify_type = 'job_detailed'";
$row3 = mysqli_query($db_manager->connection,$notify_query3);
$jcount = mysqli_num_rows($row3);
$notify_query4 = "select count(*) from notify where status = 1 and notify_type = 'msg_sent'";
$row4 = mysqli_query($db_manager->connection,$notify_query4);
$mcount = mysqli_num_rows($row4);
I am getting output 1 from all the four queries:
Please help me.
Use fetch_row() instead mysqli_num_rows().
$result = $db->query("select count(*) from notify where status = 1 and notify_type = 'resume_uploaded'");
$row = $result->fetch_row();
echo 'No of count: '. $row[0];

PHP Sum a value in while loop, but with conditions

I have two tables to be joined, 1 is user and 1 is attendance.
TABLE : attendance
id userId totalHours
1 1 0745
2 3 0845
3 1 0945
TABLE : user
id name departmentId
1 John 2
2 Sean 2
3 Allan 2
Not every user have attendance record (their totalHours)
But I need to query by userId WHERE departmentId = XXXX and SUM each of their totalHours that exist, without neglecting the userId without any record in attendance.
So far I made this:
$result = mysqli_query($con,"SELECT * FROM user WHERE departmentId = 2");
while($row = mysqli_fetch_array($result))
{
$id = $row['userId'];
$result2 = mysqli_query($con,"SELECT * FROM attendance WHERE userId = $id");
while($row2 = mysqli_fetch_array($result2))
$totalHours = 0;
{
$totalHours = $row2['totalHours'];
$grandTotal += $totalHours;
$totalHoursInHHmm = substr_replace($totalHours,":",2,0);
$parsed = date_parse($totalHoursInHHmm);
$toSeconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60;
$total += $toSeconds;
$init = $total;
$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
}
echo "$hours:$minutes";
}
The result shows all the user in the department, and did SUM all the totalHours for each userId , but what was wrong is, there are userId without any attendance still have the SUM value shown, inheriting previous total Sum
Any help is appreciated :)
I need to query by userId WHERE departmentId = XXXX and SUM each of
their totalHours that exist, without neglecting the userId without any
record in attendance.
To show the hours for all users in a given department, even users w/o rows in the attendance table, use a LEFT JOIN
Use (CAST(totalHours AS UNSIGNED) % 100)/60 + FLOOR(CAST(totalHours AS UNSIGNED)/100) to convert your varchar hours+minutes to a single number of hours.
$query = "SELECT u.id,
SUM((CAST(totalHours AS UNSIGNED) % 100)/60 + FLOOR(CAST(totalHours AS UNSIGNED)/100)) grandTotal
FROM user u
LEFT JOIN attendance a
ON u.id = a.userId
WHERE u.departmentId = 2
GROUP BY u.id";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)) {
print $row['id'] . ' ' . $row['grandTotal'];
}
try this, just in the first while you wont need both.
SELECT TIME_FORMAT(sum(STR_TO_DATE(a.totalHours, '%i')),'%H:%i') as sum, u.id, u.name FROM user AS u LEFT JOIN attendance AS a ON a.userId = u.id WHERE u.departmentId = 2 AND u.id = $user_id GROUP by u.id;
Update, try that not sure if it will work I cant test it right now but refer to this question.
how to convert weird varchar "time" to real time in mysql?
Once you get the right query working it will be really easy in php to do the rest. The DB should do this work, although the schema is not ideal here..
OK! It's happening because, the users that doesn't have any attendance isn't passing through the second while, then the values aren't being restarted. You can correct this simply setting $grandTotal after you echo it. Like this:
$result = mysqli_query($con,"SELECT * FROM user WHERE departmentId = 2");
while($row = mysqli_fetch_array($result))
{
$id = $row['userId'];
$result2 = mysqli_query($con,"SELECT * FROM attendance WHERE userId = $id");
while($row2 = mysqli_fetch_array($result2))
{
$totalHours = 0;
$totalHours = $row2['totalHours'];
$grandTotal += $totalHours
}
echo $grandTotal;
$grandTotal = 0;
}
What I understood from the question is NOT to neglect those userid even if they do not have their attandance record. In this scenario I have 2 Options to be chosen ...
1.
$result = mysqli_query($con,"SELECT * FROM user WHERE departmentId = 2");
while($row = mysqli_fetch_array($result))
{
$id = $row['userId'];
$result2 = mysqli_query($con,"SELECT * FROM attendance WHERE userId = $id");
$grandTotal=0;
while($row2 = mysqli_fetch_array($result2))
$totalHours = 0;
{
$totalHours = $row2['totalHours'];
$grandTotal += $totalHours
}
echo $grandTotal;
}
2.
$result = mysqli_query($con,"SELECT * FROM user WHERE departmentId = 2");
while($row = mysqli_fetch_array($result))
{
$id = $row['userId'];
$result2 = mysqli_query($con,"SELECT * FROM attendance WHERE userId = $id");
while($row2 = mysqli_fetch_array($result2))
$totalHours = 0;
{
$totalHours = $row2['totalHours'];
if($totalHours<=0)
$grandTotal=0;
$grandTotal += $totalHours
}
echo $grandTotal;
}
Move $totalhours = 0 within the curly braces {}.
Set $hours = $minutes = 0 at the top of the second while loop (where you set $totalhours = 0)
**If you don't reset $hours and $minutes, users who don't have attendance will get the old values.

how get and compare two mysql data?

table 1
two column >> paid - order_num1
table 2
two column >> order_num2
I want get order_num2 value from table 2 and
update paid(insert paid = 1) in table one with same order_num value
If order_num1=order_num2 then paid = 1 in table 1
$q = mysql_query("select order_num2 from table2 where samevalue = samevalue ");
$x = mysql_fetch_row($q);
mysql_query("update table1 set paid=1 where order_num1='$x['order_num2']'");
But it does not work!
First get from one table and update paid from another table if order_num have same value
Try
$table2 = mysqli_query("SELECT * FROM table2");
$row = mysqli_fetch_array($table2);
$num2 = $row['order_num2'];
$table1 = mysqli_query("SELECT * from table1");
$roww = mysqli_fetch_array($table1);
$num1 = $row['order_num1'];
if ($num2 == $num1) {
$updateDB = mysqli_query("UPDATE table1 SET paid = 1 WHERE order_num1 = '$num2'");
} else {
//Not equal so the paid column wont get updated
}

Group By 2 Columns with similar info to link with a 2nd table

I have two columns with similar info:
column 1 = item 1, item 3, item 5
column 3 = item 3, item 5, item 8
I want to display how many of each item are in total from both columns.
I have this:
$sql = mysql_query("SELECT FirstTypeID, SecondTypeID, ThirdTypeID, DesignID, COUNT(DesignID) FROM designs WHERE Approved = '1' GROUP BY FirstTypeID, SecondTypeID, ThirdTypeID");
while ($row = mysql_fetch_array($sql)) {
$DesignID = stripslashes($row['DesignID']);
$FirstTypeID = stripslashes($row['FirstTypeID']);
$SecondTypeID = stripslashes($row['SecondTypeID']);
$ThirdTypeID = stripslashes($row['ThirdTypeID']);
$Total = stripslashes($row['COUNT(DesignID)']);
}
$result2 = mysql_query("SELECT * FROM types WHERE TypeID = '$FirstTypeID' OR TypeID = '$SecondTypeID' OR TypeID = '$ThirdTypeID'");
while ($row2 = mysql_fetch_array($result2)) {
echo "<li><a href='index_type.php?TypeID=".$row2{'TypeID'}."'>".$row2{'TypeName'}." (" . $Total . ")</a></li>";
}
but I'm not getting the result that I want, it's only giving me results from one column.
I'm not sure if this is what you are asking for but well here it is
$typesIDs = array(type0 => "", type1 => "", type2 => ""...);
foreach($typesID as $index=>$value){
$query = "SELECT COUNT(*) AS total FROM designs COLUMN1 = ".$index." OR COLUMN2 = ".$index;
$sql = mysql_query($query);
$row = mysql_fetch_array($sql);
array[$index] => $row["total"]
}

Categories