I have a php function to get dates for a morris pie chart and it was working fine.
But now that I have more data (date for the last year and the first 3 months of this year). It's now displaying duplicate months. In this case february of last year and this years data is now showing on the same pie chart.
I'd like to write a some mysql code in php that will only display the last 12 months. I have the following code:
function writesql($rec) {
$year = date('Y') -1;
$month = date('m');
$lastyear = $year - $month; // I know this is a problem.It's subtracting the two variables
$sql = "";
$sql = $sql . " SELECT";
$sql = $sql . " YEAR(`value`)as 'Year',";
$sql = $sql . " MONTH(`value`)as 'Month',";
$sql = $sql . " `value` , ";
$sql = $sql . " COUNT(`value`) as 'Calls' ,";
$sql = $sql . " ROUND(SUM( `value` ),2) as 'Value'";
$sql = $sql . " FROM `table`";
$sql = $sql . " GROUP BY";
$sql = $sql . " YEAR(`value`),";
$sql = $sql . " MONTH(`value`)" ;
$sql = $sql . " ORDER BY";
$sql = $sql . " YEAR(`value`),";
$sql = $sql . " MONTH(`value`)";
$sql = $sql." WHERE (`value`)='".$lastyear."'";// I also know this is wrong too but im lost as to how to fix it.
return $sql;
}
Then the rest follows, I only want to get the data for the last 12 months how should I be executing this.
You could do WHERE value >= DATE_SUB(CURDATE(),INTERVAL 1 YEAR);
This would show 12 months up to the current date.
You are close. Make these changes
$year = date('Y') -1; // This will give last year
$sql." WHERE YEAR(`value`)='".$year."'"
Updated Query
$sql = "
SELECT YEAR(`value`) as 'Year', MONTH(`value`) as 'Month', `value`,
COUNT(`value`) as 'Calls', ROUND(SUM( `value` ),2) as 'Value'
FROM `table`
WHERE YEAR(`value`) = '$year'
GROUP BY YEAR(`value`), MONTH(`value`)
ORDER BY YEAR(`value`), MONTH(`value`)";
First off, I find this easier to read:
$sql = "
SELECT YEAR(value) Year
, MONTH(value) Month
, value
, COUNT(value) Calls
, ROUND(SUM(value),2) Value
FROM `table`
GROUP
BY YEAR(value)
, MONTH(value)
ORDER
BY YEAR(value)
, MONTH(value)
WHERE (value) = '".$lastyear."'
";
But this query is syntactically incorrect. So here's a syntactically correct version:
$sql = "
SELECT YEAR(`value`) Year
, MONTH(`value`) Month
, COUNT(value) Calls
, ROUND(SUM(value),2) Total_Value
FROM `table`
WHERE value = '".$lastyear."'
GROUP
BY YEAR(value)
, MONTH(value)
ORDER
BY YEAR(value)
, MONTH(value);
";
Now see about prepared and bound queries
Related
This question already has answers here:
MySQL Check if date range is in date range
(2 answers)
mysql : check date range
(2 answers)
Closed 4 years ago.
I have the following code, and it's working fine:
$time = time();
$todayint=date("Y-m-d", strval($time));
$sql = "SELECT * FROM " . $DB->prefix('cal_event') . " WHERE DATE(FROM_UNIXTIME(event_start))='$todayint' OR DATE(FROM_UNIXTIME(event_end))='$todayint' ORDER BY event_organiser ASC";
How do I change the code to select data between a date range?
event_start and event_end are stored as int.
I tried using between but I'm not sure where exactly to put between in the code.
For queries against a single date field, you can make use of the BETWEEN operator:
$sql = "SELECT * FROM " . $DB->prefix('cal_event') . " WHERE
DATE(FROM_UNIXTIME(event_time)) BETWEEN '$date1' AND '$date2'
ORDER BY event_organiser ASC";
And for queries against multiple date fields (like both the start time and end time), you'll want to check => and <=:
$sql = "SELECT * FROM " . $DB->prefix('cal_event') . " WHERE
DATE(FROM_UNIXTIME(event_start)) >= '$todayint' AND
DATE(FROM_UNIXTIME(event_end)) <= '$todayint'
ORDER BY event_organiser ASC";
In both cases you'll want to make use of AND rather than OR.
Using BETWEEN (column BETWEEN value1 AND value2)
$time = time();
$todayint=date("Y-m-d", strval($time));
$sql = "SELECT * FROM " . $DB->prefix('cal_event')
. "WHERE DATE(FROM_UNIXTIME(event_start))
BETWEEN '$todayint' AND '$todayint'
ORDER BY event_organiser ASC";
Using >= (column >= value1 AND column <= value2)
$time = time();
$todayint=date("Y-m-d", strval($time));
$sql = "SELECT * FROM " . $DB->prefix('cal_event')
. "WHERE DATE(FROM_UNIXTIME(event_start)) >= '$todayint'
AND DATE(FROM_UNIXTIME(event_end)) <= '$todayint'
ORDER BY event_organiser ASC";
I would like to get data from two tables in MySQL, ordered by date.
$sql = "
SELECT
items.*, invoice.*
FROM
items
JOIN
invoice
ON
items.user_id = invoice.buyer_id
WHERE
items.user_id = '$user_id'"
LIMIT
10
ORDER BY
date;
";
Try with:
$sql = "SELECT *";
$sql .= " FROM items, invoice";
$sql .= " WHERE items.user_id = invoice.buyer_id";
$sql .= " AND items.user_id = '$user_id'";
$sql .= " ORDER BY date DESC";
$sql .= " LIMIT 10";
Also it is better if you use it as a prepared statement instead of having the variable inside the SQL query to avoid SQL injection.
Is this possible to do?
I have a query that is selecting all records from a table, which the results is based on a time field. This is a schedule of games for 17 weeks. What I am trying to do is create an array of just the current weeks games that have expired. In my code below, every expired game shows in the array. Is it possible to just create this separate array to only include the current week games? I still need to capture all the data, but am trying to create an array of data just for the current week as seen in the row expired portion of the code.
$latestweek = getCurrentWeek(); //gives the current week
for($wk=1;$wk<=17;$wk++){
$games = array();
$sql = "select s.*, (DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > gameTimeEastern or DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > '" . $cutoffDateTime . "') as expired ";
$sql .= "from " . DB_PREFIX . "schedule s ";
$sql .= "where s.weekNum = " . $wk . " ";
$sql .= "order by s.gameTimeEastern, s.gameID";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
$e = 0;
while ($row = $query->fetch_assoc()) {
$games[$row['gameID']]['gameID'] = $row['gameID'];
$games[$row['gameID']]['homeID'] = $row['homeID'];
$games[$row['gameID']]['visitorID'] = $row['visitorID'];
$games[$row['gameID']]['sWinner'] = $row['result'];
$games[$row['gameID']]['startTime'] = $row['gameTimeEastern'];
if ($row['expired']){
$survGamesExpired_h[$e]=$row['homeID'];
$survGamesExpired_v[$e]=$row['visitorID'];
}
$expiredTeams = array_merge((array)$survGamesExpired_h,(array)$survGamesExpired_v);
}
}
I want to count a record by current date from different tables and return as one row with different column in the new table. The code will update a record every three hours and insert new record if current date changes. I've current date and time data (2013-05-20 14:12:12) in "created_at" column. Here my current code:
require_once('./db_connect.php');
$dbcon = new db;
//test to see if a specific field value is already in the DB
public function in_table($table,$where) {
$query = 'SELECT * FROM ' . $table . ' WHERE ' . $where;
$result = mysqli_query($this->dbh,$query);
$this->error_test('in_table',$query);
return mysqli_num_rows($result) > 0;
}
//running in background
while (true) {
$select= "SELECT (SELECT CURDATE()) AS time," .
"(SELECT COUNT(tweet_id) FROM tweets WHERE created_at= 'CURDATE() %') AS total_count," .
"(SELECT COUNT(fid) FROM fun WHERE ftime= 'CURDATE() %') AS f_count," .
"(SELECT COUNT(sid) FROM sad WHERE stime= 'CURDATE() %') AS s_count";
$results = mysqli_query( $dbcon, $select );
while($row = mysqli_fetch_assoc($result)) {
$time = $row['time'];
$total = $row['total_count'];
$fcount = $row['f_count'];
$scount = $row['s_count'];
$field_values = 'time = "' . $time . '", ' . 'total_count = ' . $total . ', ' . 'fun_count = ' . $fcount . ', ' . 'sad_count = ' . $scount;
if ($dbcon->in_table('count','time= "' . $time . '"')) {
$update = "UPDATE count SET $field_values WHEN time= '$time'";
mysqli_query( $dbcon, $update );
}
else {
$insert = "INSERT INTO count SET $field_values";
mysqli_query( $dbcon, $insert );
}
}
//update record every 3 hour
sleep(10800);
}
With this code I can't get a count record. The result return | 2013-05-18 | 0 | 0 | 0 |. How can I correct this?
I not familiar with PHP, but you can retrieve the count of all records dated any time today using:
SELECT COUNT(tweet_id)
FROM tweets
WHERE created_at >= curDate()
AND created_at < date_add(curDate(), interval 1 day)
It is equivalent to saying
..
WHERE created_at >= (today at midnight *incusive*)
AND created_at < (tomorrow at midnight *exclusive*)
Update:
The advantage of this method is it is index friendly. While using WHERE DATE(Column) = currDate() works, it can prevent the database from using indexes on that column, making the query slower.
Replace the parts where you have this:
WHERE created_at= 'CURDATE() %'
with this:
WHERE DATE(created_at) = CURDATE()
Your existing WHERE clause is comparing created_at to the string constant CURDATE() %, and they'll never match.
You are comparing against created_at= 'CURDATE() %', which is looking for that exact string, not for the result of a function. If the field created_at is a date, it will never match.
And, you are doing that for all counts.
I'm trying to update a range of dates with a Shift_ID but the Shift_ID that is entered depends upon what day of the week it is. So, for example, if I have a range of dates $from = "2012-12-01" $to = "2012-12-28" I'm trying to make it so that I can select a check box (for example: value="Fri" name = "offdays[]") to indicate what day is an offdays. If an offdays is matched with a day in the set range, then the Shift_ID = "6" otherwise it is a work day and Shift_ID = "3"
Hopefully this will all make sense in a minute, I'm doing my best to explain it as well as give you some useful variables.
So here is my code:
if(isset($_POST['submit']))
{
if(isset($_POST['offdays']))
{
//$off is set through the config settings//
$shift = mysqli_real_escape_string($_POST['shift']);
$from = mysqli_real_escape_string($_POST['from']);
$to = mysqli_real_escape_string($_POST['to']);
$emp_id = mysqli_real_escape_string($_POST['emp_id']);
foreach($_POST['offdays'] as $checkbox)
{
echo "Checkbox: " . $checkbox . "<br>";//error checking
$sql = ("SELECT Date FROM schedule WHERE (Date BETWEEN '$from' AND '$to') AND (Emp_ID = '$emp_id')");
if(!$result_date_query = $mysqli->query($sql))
{
echo "INSERT ERROR 1 HERE";
}
echo "SELECT SQL: " . $sql . "<br>";//error checking
while($row = $result_date_query->fetch_assoc())
{
echo "Date: " . $row['Date'] . "<br>";//error checking
$date = date('D',strtotime($row['Date']));
if($date == $checkbox)
{
echo "MATCHED! Date: " . $date . " Checkbox: " . $checkbox . "<br>";//error checking
$sql = ("UPDATE schedule SET Shift_ID = '$off' WHERE Date = '" . $row['Date'] . "' AND Emp_ID = '$emp_id'");
if(!$result_update_offdays_query = $mysqli->query($sql))
{
echo "INSERT ERROR 2 HERE";
}
echo "UPDATE DAYS OFF SQL: " . $sql . "<br><br>";//error checking
}
else
{
echo "NOT MATCHED! Date: " . $date . " Checkbox: " . $checkbox . "<br>";//error checking
$sql = ("UPDATE schedule SET Shift_ID = '$shift' WHERE Date = '" . $row['Date'] . "' AND Emp_ID = '$emp_id'");
if(!$result_update_shift_query = $mysqli->query($sql))
{
echo "INSERT ERROR 3 HERE";
}
echo "UPDATE SHIFT SQL: " . $sql . "<br><br>";//error checking
}
}
}
}
}
When I look at my printed echo statements, everything is perfect! When a date lands on a Friday, it shows that it's entering Shift_ID = "6" and any other day shows Shift_ID = "3". The problem is the tables don't reflect what my statements are telling me, they all just get updated to Shift_ID = "3".
In the end I want to be able to select more than one offdays, but when I select more than one, it overwrites my previous day during the next loop, which makes sense.
I've got no idea what is going on, if anyone can give me a clue, I would really appreciate it.
UPDATE: When I add exit; after the days off update, like this:
echo "UPDATE DAYS OFF SQL: " . $sql . "<br><br>";//error
exit;
it does update the day off to Shift_ID = "6", so it must be something happening after that.
EDIT: It appears as though the problem was with user permissions. I can't explain why, but after deleting the user and creating a new one with full permissions, things started to work. I chose #andho's answer as he helped me get to the answer in the chat forum and also added a way to clean up my code.
This doesn't solve your issue, but simplifies the solution!
if your offdays variable is as follows: $offdays = array('Fri', 'Sun');
You can first set all dates in range to $shift in one query:
UPDATE Schedule SET Shift_ID = '$shift' WHERE Date BETWEEN '$from' AND '$to' AND Emp_ID = '$emp_id'
Then you can loop through the foreach ($offdays as $offday) { and update those offdays:
UPDATE Schedule SET Shift_ID = '$off' WHERE Date BETWEEN '$from' AND '$to' AND Emp_ID = '$emp_id' AND DATE_FORMAT(Date, '%a') = '$offday'
That should update all the $offday's in the range to $off.
This will reduce your loops and queries, which gives leaner code.
$sql = ("SELECT `Date` FROM schedule WHERE (`Date` BETWEEN '$from' AND '$to') AND (Emp_ID = '$emp_id')");
Use backtick(`) to all the fieldname date because it is reserved in mysql.