Need help for Mysql selected query - php

I have a field on database, the name is Time_message, and I set default for this date is w, d-m-Y H:i:s, and I wanna create some search using BETWEEN and a value just show "d-m-Y" how can i show it to my query??
I bring my code below..
$date1 = "21-08-2015";
$date2 = "d-m-Y";
$query = mysql_fetch_array(mysql_query("SELECT COUNT(*) as jumlahdata FROM message_complaint WHERE (status_message='".$data['status_message']."') AND (time_message BETWEEN $date1 AND $date2)"));

you are conflicting MySQL and MySQLi. Use MySQLi
$date1 = "21-08-2015";
$date2 = "d-m-Y";
$message = $data['status_message'];
$query = mysqli_query($con, "SELECT COUNT(*) as jumlahdata FROM message_complaint WHERE (status_message='$message') AND (time_message BETWEEN '$date1' AND '$date2')");
$row = mysqli_fetch_array($query, MYSQLI_ASSOC);
$con equal to mysqli connection

Related

Why does this MySQL Update query not work?

Here is my code for updating the date field for all records that have the field WorkoutID = $currentWorkoutID. When I run the query the dates change to 0000-00-00 and not to the current date. How can I fix this? Btw DB::getInstance() executes the query. I think something is wrong with the actual query?
$currentWorkoutID = $_SESSION['GlobalWorkoutID'];
echo $currentWorkoutID;
$date = date("y/m/d");
echo $date;
$sql = "UPDATE workout SET Date = ".$date." WHERE WorkoutID = ".$currentWorkoutID."";
DB::getInstance()->query($sql);
include single quotes ' for date
$sql = "UPDATE workout SET Date = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
^ ^
i agrre with the answer
$sql = "UPDATE workout SET Date = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
because maybe the type of Date is String ,
hope to help you,thank you
Try this
$sql = "UPDATE workout SET `Date` = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
Because date is a reserved keyword

Change DateTime from mySql to timestamp in php

In my mysql database dateTime is stored in the following form.
00000-00-00 00:00:00
In my php I want to convert it to tiemStamp form like this.
136716425
I tried using
$date2->getTimestamp();
without success, what function should I use to change the format to timestamp?
$sql = "SELECT * FROM temp_user";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$date1 = new DateTime();
$date2 = $row[dateTime];
echo $date1->getTimestamp();
echo $date2->getTimestamp();
}
MySQL has a builtin function for that called UNIX_TIMESTAMP
SELECT UNIX_TIMESTAMP(NOW())
SQLFiddle Demo
UNIX_TIMESTAMP
UPDATE
$sql = "SELECT *,UNIX_TIMESTAMP(dateTime) unx FROM temp_user";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
$var = $row['unx'];
// other codes
}

MySql delete operation comparing datetime with current date time

I am new in PHP and MySql. I want to delete rows from a table where order_before passed the current date and time. the order_before is date/time type.
<?php
$db = new PDO('mysql:host=localhost;dbname=db','root','root');
if(filter_has_var(INPUT_POST, 'submit')) {
$now = date("Y-m-d H:i:s");
$r = $db->query("SELECT * FROM db WHERE datetime < '".$now."'");
$n = $r->rowCount();
if ($n){
while($o = $r->fetchObject()) {
$db->query('DELETE FROM db WHERE id = '.$o->id);
}
}
}
Maybe something like the above? (untested, and off the top).
Just add the condition in the statement, should be easy enough:
$sql = sprintf("DELETE FROM mytable WHERE order_before > '%s'", date('Y-m-d H:i:s'));
$db->exec($sql);

Prevent repetitive rows

I have this table :
I would like to delete same rows. For example first five rows are the same, my table should have only one row that includes this data : 40.792274 29.412994 2011-12-21 17:19:52.
So I used the following code :
$query = "SELECT * FROM table GROUP BY date";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$date = $row['date'];
$lat = $row['latitude'];
$lon = $row['longitude'];
$query = "SELECT * FROM table WHERE date='$date' AND latitude='$lat' AND longitude='$lon'";
$re = mysql_query($query);
$number = mysql_num_rows($re);
$number--;
$query = "DELETE * FROM table WHERE date='$date' AND latitude='$lat' AND longitude='$lon' LIMIT $number";
mysql_query($query);
}
But this code doesn't work.. What should I do ?
Edited :
I solved my question :
$query = "SELECT * FROM table GROUP BY date";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$date = $row['date'];
$lat = $row['latitude'];
$lon = $row['longitude'];
$query = "SELECT * FROM table WHERE date='$date' AND latitude=$lat AND longitude=$lon";
$re = mysql_query($query);
$number = mysql_num_rows($re);
$number--;
$query = "DELETE FROM table WHERE date='$date' AND latitude=$lat AND longitude=$lon LIMIT $number";
mysql_query($query);
}
Query lines were incorrect in my first question.
To remove the duplicate elements, you would use something like this:
$q = "SELECT date FROM table GROUP BY date"
$r = mysql_query($r);
$date = '';
while($row = mysql_fetch_array($r)){
$date = $row['date'];
$q = "SELECT date FROM mytable WHERE date='$date'";
$re = mysql_query($q);
$num = mysql_num_rows($re);
$num--;
$q = "DROP FROM mytable WHERE date='$date' LIMIT $num";
mysql_query($q);
}
Should do the trick. More specifically, when creating your $date value, you have to provide PHP with a time to use. date() defaults to using the current time, but you can provide it with a custom time as the second argument.
I suggest you take a look at the strtotime() manual at php.net as well (To translate times in your db to timestamps that can be used with date() ).
EDIT: The Answer above has been edited to remove all duplicate entries.
Try changing $dateOfNewData = date('Y-m-d H:i:s');
to
$dateOfNewData = date('Y-m-d 00:00:00'); //or change the first 00 to H if you need it to match by hour, second 00 to i if you need to match minutes and the same with seconds.
or $dateOfNewData = date('Y-m-d') which is pretty much the same and works with datetime field types
And you also need to modify your query to something like this unless you need an exact time:
"SELECT * FROM mytable WHERE date = '$dateOfNewData'" // you might also want the end date if you're working with the past in your database.
Well you can try like "Ignas" suggest but you cal also try this:
First just get the date (year, month, day) without hour, minutes and seconds. If you use full date format then you need to match exactly the same time. (to second the same) which is not really what you are looking for i guess. So you can use this:
$dateOfNewData = date('Y-m-d'); //just get year, month, day in right format (2011-12-20)
Then run a query. Here you have more options but i think the easier is something like that:
"SELECT * FROM mytable WHERE date_col LIKE '$dateOfNewData%' GROUP BY date_col"
This will group the same dates together and will display just once and will match all the rows where 'date_col starts with example: 2011-12-20% (thats why i use LIKE and $dateOfNewData%)
$dateOfNewData contains current date in this format:year-month-day (2011-12-20) and in Mysql query dont forget to use % at the end of the date. It's like * in windows for example.
'mytable' replace with your table name and 'date_col' with date column.
date() you have used will give current date time , so try to use mktime() to get extact date time you want.
you have to change your query little bit, I have modified query below,
$query = mysql_query("SELECT * FROM mytable WHERE date='$dateOfNewData'");
In mysql Date or datetime coulmn should be within ''.

PHP <<< (multi-line handler?) question

I have some code as follows:
$query = <<<QUERY
SELECT
*
FROM
names
WHERE
create_date > date('Y-m-d H:i:s');
QUERY
How can I put the date('Y-m-d H:i:s') in there without breaking out of the <<< statement?
You could store that piece of code in a variable and use substitution.
$now = date('Y-m-d H:i:s');
$query = <<<QUERY
SELECT
*
FROM
names
WHERE
create_date > $now;
QUERY;
(Example: http://www.ideone.com/pKSVF)
$date = date('Y-m-d H:i:s');
$query = <<<QUERY
SELECT
*
FROM
names
WHERE
create_date > $date
QUERY
http://en.wikipedia.org/wiki/Here_document#PHP

Categories