I got a question regarding if I want update 2 record by only one command, isn't possible do like this ?
mysql_query("UPDATE damacaicheck SET trimresult = '$trim' where date = '$Current' and date = '$yesterdaydate'");
or
mysql_query("UPDATE damacaicheck SET trimresult = '$trim' where date in ('$Current','$yesterdaydate')");
Instead of date = '$Current' AND date = '$yesterdaydate'" use date = '$Current' OR date = '$yesterdaydate'"
Your first example needs to use OR rather than AND.
There was a no issue on your query problem is its may not return any value use OR instead of AND as previous posters said
Related
I have the following statement in the where statement of a mysql query:
WHERE scrap_date LIKE '%-01-%'
I want to grab all the data with the scrap_date being in January. The error I recieve is:
"Incorrect datetime value: '%-01-%' for column 'scrap_date' at row 1"
The datatype for scrap_date is DATETIME.
Not sure what syntax to use to get data with the a date in January, any suggestions?
You are assuming the date is represented internally as a string. It is not.
Since its a DateTime, use the MONTH function to extract the month and compare it to the desired value
WHERE MONTH(scrap_date) = 1
You may try this:
select * from your table WHERE MONTH(scrap_date) = 1
You can use the DATEPART() function
SELECT * FROM table
WHERE (DATEPART(mm, scrap_date) = 01)
use month function
WHERE MONTH(scrap_date) = 1
I'm trying to filter out repeated values entering into a MySQL table, by comparing the input PHP variable with the timestamp of an entry already present in the table and only if they don't match, the input PHP variable is entered into the table.
$user1_date = mysql_real_escape_string($user1_date); // the date variable
$user1_temp1 = mysql_real_escape_string($user1_temp1);
$user1_temp2 = mysql_real_escape_string($user1_temp2);
$user1_temp3 = mysql_real_escape_string($user1_temp3);
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date)); //Typecasting PHP variable into timestamp
$sql_check = "SELECT * FROM user_details WHERE temp_date ='$user1_date'";
$result_check = mysql_query($sql_check);
$num_rows_check = mysql_num_rows($result_check);
if ($num_rows_check == 0) // To check if there is no entry in the table with the same date and time as input PHP variable
{
$sql_insert = "INSERT INTO data_hour (user_id, temp1, temp_date, temp2, temp3)
VALUES (1,'$user1_temp1', '$user1_date', '$user1_temp2', '$user1_temp3')";
$result_insert = mysql_query($sql_insert);
}
temp_date is a column in the table of type timestamp. Even when the $user1_date is the same as the temp_date(timestamp) column for one of the entries in the table, it considers it as not equal and is inserting it into the table and hence I'm getting repeated values. I'm guessing the WHERE temp_date = '$user1_date'is not working properly. Some troubleshooting that I have done included
Changing '$user1_date' to just $user1_date in the WHERE
statement
Changing the WHERE clause as follows WHERE temp_date = (date)'$user1_date'
It will be great if somebody can help me out with this!
A nice easy solution would be giving temp_date a UNIQUE INDEX in your Mysql Table, as that would not allow the same value to be inserted twice. This would also make your operations more efficient, as you wouldn't have to do SELECT * every time you want to insert something.
However, for what you're doing, I think I see your problem; there are some quirks in your logic so I'll try to dispel them here. (Hopefully?) this will make your program cleaner and you'll be able to pinpoint the error, if not eliminate it altogether.
Examining this piece of code:
// $user1_date doesn't have a value here! //
$user1_date = mysql_real_escape_string($user1_date);
...
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date));
Error 1 - You escape the string before ever setting a value.
What you are doing is that you are using mysql_real_escape_string() before $user1_date is ever defined.
Correction:
// Getting better, but not done. //
$user1_date = date("Y-m-d H:i:s", strtotime($user1_date));
...
$user1_date = mysql_real_escape_string($user1_date);
Error 2 - You do not give the date() function appropriate parameters
The date() function in PHP expects a timestamp, which is just an int. You can easily get the time with time(), so that should rectify your problem
Correction:
// You use strtotime($user1_date), but you should use time() //
$user1_date = date("Y-m-d H:i:s", time());
...
$user1_date = mysql_real_escape_string($user1_date);
These are small mistakes, but they can be deadly. Like I said, you should assign temp_date to a UNIQUE INDEX in your MySQL table, but make sure to correct these errors listed as well.
Let me know how it goes!
I am trying to compare a stored date to the current date, and send back data accordingly. I'm not fluent in PHP so this is what I have so far
while ($row = mysql_fetch_array($result)) {
// temp user array
$event_date_str = $row["date"];
$todays_date_str = date("m-j-Y");
$today = strtotime($todays_date_str);
$event_date = strtotime($event_date_str);
if($event_date > $today){
$event = array();
$event["pid"] = $row["pid"];
$event["name"] = $row["name"];
$event["longitude"] = $row["longitude"];
$event["latitude"] = $row["latitude"];
$event["pavement"] = $row["pavement"];
$event["traffic"] = $row["traffic"];
$event["environment"] = $row["environment"];
$event["image_b64"] = $row["image_b64"];
$event["created_at"] = $row["created_at"];
$event["date"] = $row["date"];
$event["time"] = $row["time"];
$event["type"] = $row["type"];
// push single product into final response array
array_push($response["events"], $event);
}else{
//delete it here
}
With that code right there I am getting nothing back. I have 2 items stored in the database, One with the date "2-28-2014" and another with "2-14-2014", so I should be getting one back and not the other but I am getting neither back. I know that there are no leading zeros with the dates saved so I should use j right? Could someone help me figure out why this is not working, sorry if it seems like a simple question.
Thank you in advance,
Tyler
This is not an efficient way to do things. If you need to pick items based on date, do it in the MySQL query directly. PHP filtering will always be slower than MySQL. Especially since you have to deliver extra data over network when filtering at PHP level.
So do it like this:
SELECT * FROM `table` WHERE `record_expires_datetime_gmt` > UTC_TIMESTAMP();
SELECT * FROM `table` WHERE `record_expires_date_gmt` > DATE(UTC_TIMESTAMP());
// use NOW() for local time, UTC_TIMESTAMP() is GMT/UTC
Then do what you need to do with the records. Never SELECT * and then filter records in PHP.
There's a whole set of DATETIME functions in MySQL to allow you MySQL server side filtering of data.
PS: Obviously, for this method to work, your MySQL table has to be properly designed. Date (date and time) fields need to be of type DATE or DATETIME not surrogate strings that are meaningful only within your project.
I need to create a script that compares one field in the database (has a date stored, it's type is "TEXT" and cannot be changed DATE) to the current server date.
The dates are encoded like this "1380571547", so i need to use strftime() to decode them. This field for example, decoded with strftime corresponds to this "Sep-30-2013, 22:05"
What I need is to compare those fields with the current date, and according to that condition, write something like "Expired" in another field.
To achieve this, I made this block of code:
<?php
require("connection.php");
$today = strftime('%b-%d-%Y, %H:%M');
$exp_date = mysql_query("SELECT numbers FROM date");
while($row = mysql_fetch_array($exp_date))
{
echo (strftime ( '%b-%d-%Y, %H:%M', $row ['numbers'])). "<br />";
}
if ($exp_date < $today) {
$sql = "INSERT INTO date (changed) VALUES ('EXPIRED')";
$result = mysql_query($sql);
echo "ADDED!";
}
?>
However, this code is not working, can someone help me ?
PHP is not my strong point but it looks to me like you condition is doing a comparison on an array,
IE:
if ($exp_date < $today) // will always be false.
Your code would probably have to look something more like this.
while($row = mysql_fetch_array($exp_date))
{
if ($row[0] < $today)
{
$sql = "Update date set changed = VALUE where rowid = rowid";
$result = mysql_query($sql);
echo "ADDED!";
}
}
having said that i would probably do the comparison and update in SQL using a case statement,
Update Date
set changed = case when number > ExpiryDate
then "Expired"
else "Current"
end
You can do all this in a single query:
UPDATE `date` set `changed`='Expired' where date(now()) > date(from_unixtime(`numbers`))
But this is not what your code is attempting to do. Your second block seems to be inserting the word Expired in new rows, rather than updating anything.
Note that the table name date should be wrapped in backticks to avoid any possible clash with MySQL keywords
I don't understand the second block of code with the insert. I would do an update inside the loop. but if your going to do that, it could probably be done in one combined update statement.
Google hasn't been much help sadly. I have some pseudo code below to give you an idea of what I'd like to achieve:
if ($time == one week) {
$result = mysql_query("SELECT * FROM table RANDOMLY,$connection");
echo $result[0];
}
I know I should be using mysqli, but I'm augumenting an existing (ageing) system. I'll be utilising mysqli in future, so if you could give me the solution using mysql that would be great!
I don't think it can be done with a single statement.
My best guess would be to use mysql_list_tables, select a random entry from that list and continue from there on.
Perhaps generating a random number between 1 and the value of SELECT Count(ID) from table. Then you have the index of the value you wish to output, you can simply run a SELECT statement for it; and output! :)
if ($time == one week) {
$result = mysql_query("SELECT * FROM table RANDOMLY,$connection");
echo $result[0];
}
Try this:
$weekNumber = date("W");
$result = mysqli_query("SELECT * FROM table RANDOMLY WHERE weeknumber = '\"$weekNumber\"', $connection");
echo $result[0];
}
Of course you’ll need a column in your table called weeknumber with 1 through 52 setup ahead of time.
As others pointed out you shouldn’t use mysql_* anything. I changed it to a mysqli_query.