I have a sql query which has an if..else function. if the submit button is clicked, it will perform the particular query. else, the query will take in the first 20 dates in my database. however, i am not sure how to do the else query statement to take up only the first 20 dates. do help, thank you :)
<?php
$server = "localhost";
$user = "root";
$password = "";
$database = "sensors_database";
$connection = mysql_connect($server,$user,$password);
$db = mysql_select_db($database,$connection);
if(isset($_POST["usub"])){
$date1 = $_POST["datepicker"];
$date2 = $_POST["datepicker1"];
$query1 = "SELECT time, Ultrasonic FROM pi_sensors_network WHERE date BETWEEN '".$date1."' AND '".$date2."'";
$result1 = mysql_query($query1);
while($row = mysql_fetch_assoc($result1))
{
$dataset1[] = array(strtotime($row['time'])*1000,$row['Ultrasonic']);
//echo strtotime($row['time'])*1000;
}
}
else {
}
?>
Here you go:
SELECT `time`, `Ultrasonic` FROM `pi_sensors_network`
WHERE 1=1 ORDER BY `date` ASC LIMIT 0,20
If you are asking between the dates you mentioned in the query, then use
limit
$query1 = "SELECT time, Ultrasonic FROM pi_sensors_network WHERE date BETWEEN '".$date1."' AND '".$date2."' limit 0,20";
First Create index on table < pi_sensors_network > on column < date >
CREATE INDEX pi_sensors_network01 ON pi_sensors_network (`date`);
Then,
SELECT
`time`,`Ultrasonic`
FROM
pi_sensors_network
WHERE
`date` IN ( SELECT `date` FROM
(SELECT DISTINCT(`date`)
FROM pi_sensors_network
ORDER BY `date` ASC LIMIT 0,20)
AS tmp ) ;
If your table is too large, This query may take long even after indexing,
In this case,
I'ld suggest you to make an array of first 20 dates by separate query and then implode it in your main query !
Related
My calendar table has start and end column and they have the same data type which is date. Now I'm having trouble on how to query and select the data in desc order according to the date recorded in the start column. Does anyone know how does my query should be?
I only have this:
//code indentation
$query = "select * from calendar";
$res = mysqli_query($conn,$query);
while($row=mysqli_fetch_array($res))
{
...
}
EDIT:
The right query is
$query = "select * from calendar ORDER by start DESC";
Thanks to everyone who answered! :)
Does just doing a simple ORDER BY not work?
$query = "select * from calendar ORDER BY start DESC";
$query = "select * from calendar ORDER BY start DESC";
$res = mysqli_query($conn, $query);
while( $row=mysqli_fetch_array($res) )
{
// your code
}
I think this should work. You can use ORDER BY.
$query = "select * from calendar ORDER BY date1, date2 DESC";
Mysql will first check for date1 if it is blank it will go for date2
How do I add all the amounts together so that instead of this select script returning all the individual withdrawal amounts return one number of them all added together?
<?php
$end_date = strtotime($nextdate);
$start_date = strtotime($statement_date);
$username = $usr['username'];
$result = mysql_query("SELECT * FROM `usr_withdraw` WHERE `timestamp` > '$start_date' AND `timestamp` < '$end_date' AND `username` = '$username'")or die(mysql_error());
while($balance = mysql_fetch_array( $result )) {
echo $balance['amount'];
}
?>
To return only 1 row with the aggregate sum of all amounts use the sql sum() function.
SELECT sum(amount) FROM `usr_withdraw` WHERE ...
Do
$result = mysql_query("SELECT SUM(amount) FROM `usr_withdraw` WHERE `timestamp` > '$start_date' AND `timestamp` < '$end_date' AND `username` = '$username'")or die(mysql_error());
But also keep in mind that these mysql_* functions are out of PHP from version 7 on, then your code will be soon out of date. The ideal is using mysqli_* functions or PDO.
In my table i have date records like 02-04-2016 , 03-01-2016 and 04-01-2016 if i am on 03-01-2016 i want the previous record which is 02-01-2016 But it gives me 01-01-2016 which is the first record of my table. No matter what date i am on.
if(isset($_POST['place'])){
$place = $_POST['place'];
$date = date("Y-m-d", strtotime($_POST['date']));
$classtype = $_POST['classtype'];
$getdate = mysql_query("SELECT * FROM `class` WHERE `city`='$place' AND `clastype`='$classtype' AND `classdate`<'$date' limit 0,1")or die(mysql_error());
$mydt = mysql_fetch_array($getdate);
$mdt = date("d-m-Y", strtotime($mydt[classdate]));
echo $mdt;
}
"SELECT * FROM `class` WHERE `city`='$place' AND `clastype`='$classtype' AND `classdate`<'$date' order by `classdate` desc limit 0,1"
Please use order by clause.
Use ORDER BY clause
Try this:
SELECT *
FROM `class`
WHERE `city`='$place' AND `clastype`='$classtype' AND
`classdate`<'$date'
ORDER BY id DESC
LIMIT 0,1
Why isn't php subtracting $ltimeout from $ltimein? It just returns $ltimein. Here's my code:
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$querytimein = mysql_query("
SELECT timein
FROM studentInfo
WHERE name = '$name'
ORDER BY time DESC
LIMIT 1
")
or die("Error querying database ".mysql_error());
$querytimeout = mysql_query("
SELECT timeout
FROM studentInfo
WHERE name = '$name'
ORDER BY time DESC
LIMIT 1
")
or die("Error querying database ".mysql_error());
while($minutestimein = mysql_fetch_array($querytimein)){
$ltimein = $minutestimein['timein'];
}
while($minutestimeout = mysql_fetch_array($querytimeout)){
$ltimeout = $minutestimeout['timeout'];
}
$timegone = $ltimein - $ltimeout;
echo $timegone;
}
BTW I know I need to switch to mySQLi, I will do that before I publish my website.
Since you are retrieving only a single row use
$ltimein = mysql_fetch_row($querytimein);
$ltimeout = mysql_fetch_row($querytimeout);
And then you could find the difference like this:
if(isset($ltimein) && isset($ltimeout)){
$timediff = strtotime($ltimein['timein'])- strtotime($ltimeout['timeout']);
}
you can also do the time difference in mysql query:
select UNIX_TIMESTAMP(timein) - UNIX_TIMESTAMP(timeout) as time_diff from studentInfo where '$name' ORDER BY time DESC LIMIT 1
Im trying to do a mysql check if a record from $uid exist from today based on $timestamp and if it doesnt then do an INSERT.
//EXAMPLE RECORD FROM TABLE VOTE
--- #vote_fb_uid# --- #vote_time#
665414807 1369219044
tjt
//STEP 1 - do a look up on $uid and check with timestamp $today
$timestamp = $this->time;
$date = date('Y-m-d', $timestamp);
$today = date('Y-m-d');
$sql = "
SELECT * FROM vote WHERE
vote_fb_uid = '$this->fb_uid',
WHERE vote_time = '$CHECK_IF_THERE_IS_AN_ENTRY_FROM_TODAY'";
$res = mysql_query($sql) or die( mysql_error());
//STEP 2 - If no records are found for today - then we do an INSERT
if($no_record_for_today) {
$sql = sprintf("
INSERT INTO vote(
vote_fb_uid,
vote_time)
VALUES ('%s','%s')",
mysql_real_escape_string($this->fb_uid),
mysql_real_escape_string($this->time));
$res = mysql_query($sql) or die( mysql_error());
}
Obviously im strugling with the SQL part for the look up - im wondering if there isnt some in-built SQL function to do this or similar?
to check if you had a vote in the last 24 hours :
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid'
AND FROM_UNIXTIME(vote_time) >= DATE_SUB(NOW(), INTERVAL 1 DAY)
if you want to limit to the same day (mean you are allowed to post at 2013.05.21 23:55 and 2013.05.22 00:05)
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid'
AND DATE(FROM_UNIXTIME(vote_time)) = DATE(NOW())
CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.
mysql> SELECT CURDATE();
-> '2008-06-13'
mysql> SELECT CURDATE() + 0;
-> 20080613
Try this:
$today = date('Y-m-d'); //change it to timestamp if you want in timestamp
$sql = "
SELECT count(*) as total FROM vote WHERE
vote_fb_uid = '$this->fb_uid' and
vote_time = '$today'";
$res = mysql_query($sql) or die( mysql_error());
if($res[0]['total'] < 1){
$sql = sprintf("
INSERT INTO vote(
vote_fb_uid,
vote_time)
VALUES ('%s','%s')",
mysql_real_escape_string($this->fb_uid),
mysql_real_escape_string($this->time));
$res = mysql_query($sql) or die( mysql_error());
} else{
//return error("custom","","Already Inserted.");
echo "already inserted";
}
Your $sql query have a syntax error, you have used two times clause WHERE the correct syntax to use two or more clauses in where is using AND to join them, to get only record wich don't have an entry for today you can use DATE_SUB with 1 day interval
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid',
AND vote_time <= DATE_SUB(vote_time, INTERVAL 1 DAY)