MySQL query with date parameter - php

My table in mysql has special data stamp (startdata). It is date when event starts. Older events stored in database to, but i don't need them to appear in the MySQL answer.
So is there any way sending query to database that includes parameter i need? (for example, not showing rows where startdate older than today).
Now my code looks like:
$res3 = mysqli_query($con,"SELECT * FROM raspisanie WHERE
instr='" . $instrument . "' AND school IN(" . $array2 . ")
AND type='regular' AND state='1' ORDER by startdate");

Add condition in your where clause.
$res3 = mysqli_query($con,"SELECT * FROM raspisanie WHERE
instr='" . $instrument . "' AND school IN(" . $array2 . ")
AND type='regular' AND state='1' AND startdate >now() ORDER by startdate");

Related

Is it possible to combine these two SQL statements into one?

I am using these two MySQL statements to get all messages between two users, but I am running into a pretty big problem when it comes to manipulating the data later in the code since it's not in order by ID. Is there any way to combine these statements AND order the results by ID?
$sql = "SELECT * FROM messages WHERE sender='" . $username . "' AND receiver='" . $chatPartner . "'"
$sqlTwo = "SELECT * FROM messages WHERE sender='" . $chatPartner . "' AND receiver='" . $username . "'"
Essentially, I need every occurrence of a message between two people, but sorted by ID. As of right now, I am joining the arrays to get my full list, but it's not in order by ID.
SELECT * FROM messages
WHERE (sender='" . $username . "' AND receiver='" . $chatPartner . "'")
OR (sender='" . $chatPartner . "' AND receiver='" . $username . "'")
ORDER BY id DESC
How about just combine both into 1 query ?
$sql = "SELECT * FROM messages WHERE (sender=" . $username . " OR sender =". $chatPartner .") AND (receiver=" . $chatPartner . " OR receiver=" . $username .") ORDER BY id"
You could also write a shorter version of #dtj's answer using row constructors.
SELECT *
FROM messages
WHERE (sender, receiver) IN (($username, $chatPartner),($chatPartner, $username))
ORDER BY id DESC
In my opinion it looks a bit nicer in code and makes it more readable, but remember that it doesn't improve performance of execution.

Select records from MYSQL database grouped by day

I have a database of records that have a timestamp asscoiated with them. I would like to pull records from the database grouped by day, so if there are other records with the same date (24 hour span) I would like them to be grouped together. Can this be done with MYSQL or will I have to pull the records and organise them into arrays using PHP?
Here is a screenshot of my table:
Here is my model function so far:
public function getUsersMoves($options) {
$query = "SELECT * FROM myzone_user_hr_records";
if(isset($options['GUID'])) {
$query .= " WHERE GUID = '" . $options['GUID'] . "'";
}
if((isset($options['minHR'])) && isset($options['maxHR'])) {
$query .= " AND (hr BETWEEN '" . (int)$options['minHR'] . "' AND '" . (int)$options['maxHR'] . "')";
} else if (isset($options['zone'])) {
$query .= " AND zone = '" . (int)$options['zone'] . "'";
}
if(isset($options['activity'])) {
$query .= " AND title = '" . $options['activity'] . "'";
}
$query .= " ORDER BY time DESC";
$query = $this->db->query($query);
return $query->result_array();
}
And my controller code:
$moves = $this->myzone_model->getUsersMoves($options);
I want the data sorted so that these records are grouped together if they have the same date in the timestamp, for example (2012-11-20).
Thanks
try this :
SELECT *, DATE(time) AS date FROM [whatever you want] GROUP BY date
select * from TABLE where `time` like '2012-11-20%'
The option I have suggested works because the LIKE condition in the WHERE selects all dates from the time field that start with 2012-11-20. That means that it doesn't matter what time it is during the day, it will return all results for that day.
To make this work, you must remember to use LIKE and then add the wildcard at the end - %. You can also add the wildcard at the beginning if you wanted. An example to return all days (11th December) for multiple years would be:
SELECT * FROM TABLE WHERE `time` like '%-12-11%'

Put newest database item on top

I've made a microblog system (like Twitter) using PHP and MySQL. Every time someone posts an item it includes the UNIX timestamp in the database. But my question is, how do i get the newest item on top of the page, the second to newest as second, etc? So basically, the one with the 'highest' timestamp on top of the page, the one with a bit 'less' timestamp under that.
This is what I got (I know mysql_ functions are deprecated):
<?php
/*Database test.
© 2013 Sydcul. All rights reserved.
To set the database settings, use the 'install' directory*/
include ($_SERVER['DOCUMENT_ROOT'] . '/test/config.inc.php');
$connection = mysql_connect($host, $user, $password);
mysql_select_db($database, $connection);
$messages = mysql_query("SELECT * FROM `" . $prefix . "microblog`");
while($row = mysql_fetch_array($messages))
{
$names = mysql_query("SELECT first_name,last_name FROM `" . $prefix . "data` WHERE id='" . $row['id'] . "'");
$name = mysql_fetch_assoc($names);
$fullname = $name['first_name'] . " " . $name['last_name'];
echo "<h2>Posted by " . $row['id'] . " (" . $fullname . ")</h2>";
echo $row['message'] . "<br><br>";
}
mysql_close($connection);
?>
Please explain how to do it, instead of only giving me code, otherwise I and the other people on stackoverflow can't learn from it :)
Use ORDER BY in your SQL
SELECT * FROM tablename ORDER BY datefield DESC
if you have an auto_incrementing ID field, fetch your data and ORDER BY ID or else if you have a DateField, Order by that in a descending order
ORDER BY `ID` DESC
as simple as that.

Show asks that have a duedate in future (not today) and that are not completed and that have duedates of '0000-00-00'

I have a table that have tasks that have a "duedate" (to be completed by) column and a comp_status column. it also has a user id column
0 = uncomplete
1= complete
I want to do a query that shows tasks that dont have duedate of today but a duedate in the future and that have a comp_status = 0, I also want to show tasks that have a duedate of '0000-00-00'
$query = mysql_query("select * from taskdetail where userid='" . $rec['id'] . "' and comp_status='0' and duedate > '" . date("Y-m-d") . "' or duedate = '0000-00-00' order by duedate,importantlevel ASC");
For some reason some tasks that have a completed status are still showing?
Can you help?
The problem is the you have an OR in your statement: or duedate = '0000-00-00' ... because of this, any record that has this quality will show up. You need to group it with your other condition regarding the duedate, like so:
$query = mysql_query("select * from taskdetail where userid='" . $rec['id'] . "' and comp_status='0' and (duedate > '" . date("Y-m-d") . "' or duedate = '0000-00-00') order by duedate,importantlevel ASC");
Note the parenthesis ()

Select data between two dates?

I'm using a database to store logs, with a column "date" which holds the date it was inserted. The format of the date is "MM/DD/YY". Please can anyone suggest how I would SELECT data in between two certain dates. For example, I tried this:
$from_date = "01/01/12";
$to_date = "02/11/12";
$result = mysql_query("SELECT * FROM logs WHERE date >= " . $from_date . " AND date <= " . $to_date . " ORDER by id DESC");
while($row = mysql_fetch_array($result)) {
// display results here
}
But I guess this doesn't work because the dates aren't numbers. Thanks for the help! :)
Use the BETWEEN keyword:
"SELECT * FROM logs WHERE date BETWEEN '" . $from_date . "' AND '" . $to_date . "'
ORDER by id DESC"
You can cast the fields as dates and then select between from_date and to_date
SELECT * FROM logs WHERE date STR_TO_DATE(date, '%m/%d/%Y') between STR_TO_DATE(from_date, '%m/%d/%Y') and STR_TO_DATE(to_date, '%m/%d/%Y')
The answer to your question depends on the data type that is used to store the date field in the logs table.
SQL (MySQL in your case) is fully capable of comparing dates. Usually, the BETWEEN .. AND .. operator is used but that will not work correctly if the type of date is CHAR (or VARCHAR) - in which case you will need to cast the date field to a DATETIME before comparing.
You need to add single quotes to the date values '01/01/12':
$from_date = "01/01/12";
$to_date = "02/11/12";
$result = mysql_query("SELECT * FROM logs WHERE date >= '" . $from_date . "' AND date <= '" . $to_date . "' ORDER by id DESC");
Change date parameters into Unix timestamps and then compare them. Here is the code:
$from_date = "2019/01/12";
$to_date = "2019/01/15";
$from_date_unix = strtotime($from_date);
$to_date_unix = strtotime($to_date);
$result = mysql_query("SELECT * FROM logs WHERE date >= " . $from_date_unix . " AND date <= " . $to_date_unix . " ORDER by id DESC");
while($row = mysql_fetch_array($result)) {
// display results here
}

Categories