mysql count into PHP variable

Like this: // Changed the query – there’s no need for DISTINCT // and aliased the count as “num” $data = mysql_query(‘SELECT COUNT(`users_id`) AS num FROM `users_table`’) or die(mysql_error()); // A COUNT query will always return 1 row // (unless it fails, in which case we die above) // Use fetch_assoc for a nice associative … Read more

Mysql Counting the consecutive rows that match

I loaded a SQLfiddle here: http://sqlfiddle.com/#!2/5349a/1 However, in your sample data, you had two ID=5. I made it unique. Also my SQLFiddle data doesn’t match yours anymore since I changed some values to make sure it worked. Have fun with it 🙂 (This works looking at the largest ID value for the sequence) Try this: … Read more

Query for count of distinct values in a rolling date range

Test case: CREATE TABLE tbl (date date, email text); INSERT INTO tbl VALUES (‘2012-01-01’, ‘[email protected]’) , (‘2012-01-01’, ‘[email protected]’) , (‘2012-01-01’, ‘[email protected]’) , (‘2012-01-02’, ‘[email protected]’) , (‘2012-01-02’, ‘[email protected]’) , (‘2012-01-03’, ‘[email protected]’) , (‘2012-01-04’, ‘[email protected]’) , (‘2012-01-05’, ‘[email protected]’) , (‘2012-01-05’, ‘[email protected]’) , (‘2012-01-06’, ‘[email protected]’) , (‘2012-01-06’, ‘[email protected]’) , (‘2012-01-06’, ‘[email protected]`’) ; Query – returns only days where … Read more