How do I count non-null variables in php database?(SQL)
This is my existing code and database in the picture:
$jack5 = mysql_fetch_array(mysql_query("SELECT * FROM z3 WHERE ID='$users_Names' AND Score IS NOT NULL "));
echo $jack5;
when I echo the result, I get:
Notice: Array to string conversion etc... error
I'm new to this so any help is appreciated!
z3 is my table name
Score is my column name
Try this:
$result = mysql_query("SELECT * FROM z3 WHERE ID='$users_Names' AND Score IS NOT NULL "));
$rowCount = mysql_num_rows($result); // It will return the number of rows in result set
Try this,
$jack5 = mysql_query("SELECT * FROM z3
WHERE ID='$users_Names' AND Score IS NOT NULL");
while ($row = mysql_fetch_array($jack5))
{
print_r($row);
}
Count the number of rows by using COUNT() function.
SELECT COUNT(*) FROM z3
WHERE
ID='$users_Names'
AND Score IS NOT NULL
This will return you the count of rows having the given criteria in WHERE clause.
Try this:
$jack5 = mysql_fetch_array(mysql_query("SELECT count(*) FROM z3
WHERE ID='$users_Names' AND Score IS NOT NULL "));
echo $jack5;
Related
At the moment I'm able to count all the record's in a table with the value "Waiting". This is done by using:
$query = "SELECT COUNT(*) AS SUM FROM jobs WHERE status='Waiting'";
When I want to echo the row count I just do:
echo $rows['SUM'];
How can I do it so it also count's all the record's in a table with the value "Ready"?
This query will return all status count divided by status:
SELECT status, COUNT(status) AS tot FROM jobs GROUP BY status
The resulting set is something like this:
status tot
----------- ------
Waiting 123
Ready 80
... 56
So you want status='Waiting' or status='Ready'? You can separate parameters with OR or AND; for example:
$query = "SELECT COUNT(*) AS SUM FROM jobs WHERE status='Waiting' OR status='Ready'";
SELECT count(status) AS 'count' FROM jobs GROUP BY status HAVING status='Ready'
This will count the records with the status = Ready. You use GROUP BY to get an aggregate on the status field. And because you have used GROUP BY, you use HAVING (which is the equivalent of WHERE in GROUP situations) to filter the data.
By using IN operator in your query.
$query = "SELECT COUNT(*) AS SUM FROM jobs WHERE status IN ('Waiting','Ready')";
Get group based(status) counts by using GROUP BY operator
$query = "SELECT COUNT(*) AS SUM FROM jobs GROUP BY status";
Print the status based result.
//Create the `mysqli_connection` and assign to `$conn` variable.
$result = mysqli_query($conn, $query);
if($result->num_rows>0)
{
while($row = mysqli_fetch_assoc($result))
{
echo "<br>status=" . $row['status'];
echo "<br>SUM=" . $row['SUM'];
}
}
$users = mysql_query("SELECT *, COUNT(votes.id) FROM users INNER JOIN votes
ON users.id=votes.recipientuserid WHERE votes.datenumber >='2014' ");
while($user = mysql_fetch_array($users)){
$count = $user[COUNT(votes.id)];
}
In phpmyadmin the query count displays a number. The value of $count is not a number but the users.username value. Why?
One way to accomplish what you want is to modify your query by adding a field name to count so that you can access it as any other field in the results:
SELECT *, COUNT(votes.id) as nbvotes FROM users ...
And the from php once you have the results, you can access it this way
$row['nbvotes']
where $row is the variable containing the record returned from mysql. Do not forget the appostrophes.
Cheers
$users = mysql_query("SELECT COUNT(votes.id) as count FROM users INNER JOIN votes
ON users.id=votes.recipientuserid WHERE votes.datenumber >='2014' ");
while($user = mysql_fetch_array($users)){
$count = $user['count'];
}
I'm trying to select values from row 85 in this while loop.
So all values that are selected in the database before row 85 should be excluded in the while loop, and everyone above 85 should be "Do Something" with.
Any suggestions how to achieve this?
$to_emails = mysql_query("SELECT * FROM ".$DBprefix."users WHERE workouts > 10");
while ($to_email = mysql_fetch_array($to_emails)) {
// Do Something
}
Look here:
To retrieve all rows from a certain offset up to the end of the
result set, you can use some large number for the second parameter.
This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
http://dev.mysql.com/doc/refman/5.0/en/select.html
So try something like this:
$to_emails = mysql_query("
SELECT * FROM ".$DBprefix."users
WHERE workouts > 10
LIMIT 85,18446744073709551615");
Try like
$to_emails = mysql_query("SELECT * FROM ".$DBprefix."users WHERE workouts > 10 LIMIT 85,200");
Or even try like(May be it works)
$to_emails = mysql_query("SELECT * FROM ".$DBprefix."users WHERE workouts > 10 LIMIT 85,(SELECT COUNT(*) FROM ".$DBprefix."users");
I have an issue with my code. I have 2 tables. First employee_id:
|Employee id|
1
2
3
And the second table called employee_times:
|Employee_id|Hours_dev|hours_pm|
|1|2|3|
|1|3|4|
|2|3|3|
What I am trying to do is to calculate the total time that each employee has worked (hours_dev+hours_pm). For example employee_id 1 has worked 12 hours
So far I have tried to retrieve all the employee_id from the first table and use a for loop to go through the employee_times in an SQL statement (SEE CODE BELOW). However the code does not work as it prints 0 for both employee_id and total_hours.
I am using MYSQL on a localhost server.
$sql = "SELECT employee_id FROM employee";
$result = mysql_query($sql);
while($row = mysql_fetch_array)
{
$employee_id = $row['employee_id'];
}
$employee_id_length = sizeof($employee_id);
for($i = 0; $i < $employee_id_length; $i++)
{
$sql4 = "SELECT employee_id, hours_dev, hours_pm FROM employee_times WHERE employee_id= '$employee_id[$i]'";
$result = mysql_query($sql4);
while($info = mysql_fetch_array($result));
{
$employee_id = $info['employee_id'];
$hours_dev=$info['hours_dev'];
$hours_pm=$info['hours_pm'];
$total_hours = ($total_hours + $hours_dev + $hours_pm );
}
//print "$employee_id worked for $total_hours";
}
Any help is much appreciated.
you can get sum directly
select employee_id, sum(hours_dev)+ sum(hours_pm) as total
from employee_times WHERE employee_id= '1'
group by employee_id
refer this Fiddle Demo
this should get the data you need
SELECT
hours_dev,
hours_pm,
sum(hours_dev) + sum(hours_pm) as total_hours
FROM
employee_times
WHERE
employee_id = 123
GROUP BY
employee_id
Take a look at aggregate functions:
http://www.w3schools.com/sql/sql_functions.asp
http://www.w3schools.com/sql/sql_func_sum.asp
This SQL query should pull the info much quicker than by script;
SELECT Employee_id, SUM(Hours_dev), SUM(Hours_pm), SUM(Hours_dev + Hours_pm)
FROM employee_times
GROUP BY Employee_id
How can I return the 1 or 0 as true and false for my select query? I've tried every combination of fetch_assoc, arrays, etc. It usually returns as null, which doesn't make sense.
$querydetails = "select exists (select * from customer_det where id = $uid)";
$resultdetails = mysql_query($querydetails) or die(mysql_error());
while ($row = mysql_fetch_assoc($resultdetails)) {
$resultdetails1 = $row[0];
}
That's the latest version of my query. I guess it's not an array, but I'm not sure how to pull the value if there's no row to be named?
This is a post question to my previous thread: MySQL Update WHERE
SELECT IF(EXISTS(...),1,0) AS result
simple use COUNT and IF
SELECT IF(COUNT(*) = 0, 0, 1)
FROM table1
WHERE s = 2
SQLFiddle Demo
SELECT count(*) FROM (
SELECT id FROM table WHERE condition LIMIT 1
) AS result;
i am using mysql version 8.0.23 on AMS
SELECT
column1,
column2,
IF (exists(select * from MeasurementItemNos measureItem where measureItem.itemNo = ib.itemNo and measureItem.IsActive = 1),1,0) as isUploadedSizeSpec
from table
I would do:
select * from customer_det where id = $uid LIMIT 1
You will get either 1 row or zero rows.