There is a table to hold last update date in database:
$syncDate = "CREATE TABLE IF NOT EXISTS sync_info (last_sync TIMESTAMP DEFAULT '2003-01-01 00:00:00')";
And i'm updating it every time:
$dateUpdate = "UPDATE sync_info SET last_sync = NOW()";
Date is needed for data search:
$query = "SELECT * FROM results WHERE InsertionDate BETWEEN '$startDateToSearch' AND NOW()";
where insertion date is also a timestamp in table.
The problem is stupid, but I can't get proper startDateToSearch from database. It always returns integer and I can't use it for data search in that way...
$startDate = "SELECT * FROM sync_info";
$startFind = mysql_query($startDate, $GLOBALS['con']);
$array = mysql_fetch_array($startFind);
$row = $array[0];
$startDateToSearch = $row['last_sync'];
What I'm doing wrong???
Why not just use SQL in one statement, such as
SELECT * from results, sync_info
WHERE InsertionDate BETWEEN sync_info.last_sync AND NOW()
Related
When we update a MySQL record with php, we can check if it has effect using:
$mysqli->affected_rows;
But how do I check which column has been modified?
Example, in my table have the columns: id / name / age
In a record we have the data: 1 / Woton / 18
If I send an: UPDATE mytable SET name = 'Woton', age = '20' WHERE id = '1'
Only the age field has changed. How can I determine this?
You cannot directly get the updated columns from the query result.
It can be get from some php query. Firstly we will have to select the row from database which we are going to update in a array variable. Than run the update query for the same row.
Lastly get the same row from database from select query in the new array variable.
Finally we get two arrays.
We can get the updated column with the array_diff_assoc php function.
See the below code for the same.
$sql = "SELECT * from mytable where id=1 limit 1";
$prev = mysqli_fetch_assoc(mysqli_query($conn, $sql));
//Get the column data in the array. Before update.
$sql = "UPDATE mytable SET name = 'Woton', age = '20' WHERE id = '1'";
$conn->query($sql);
// Update data
$sql = "SELECT * from mytable where id=1 limit 1";
$updated = mysqli_fetch_assoc(mysqli_query($conn, $sql));
// Again run the select command to get updated data.
$UpdatedColumns=array_diff_assoc($updated,$prev);
In a different note: If QueryLog has been enabled in the DB then you (or your script in PHP or Python or any) can easily see/read which part of the content has been updated and even you can monitor the DB.
The good news is, even you can target which table, which query etc.
This is my Code
table_query
id query
1 SELECT * FROM TABLE WHERE date = "$loc_date"
<?php
$q1 = 'SELECT query FROM table_query WHERE 1 LIMIT 1';
$result = execute q1;
$query = $result['query'];
$loc_date = '2017-12-06';
$loc_query = $query; //This query fetch from table. Not directly write on file
execute query here
?>
But I don't get any result after executing query.
You have to write variable in double quotation single quote.
$loc_date = '2017-12-06';
$loc_query = "SELECT * FROM TABLE WHERE date = '".$loc_date."'";
Need to add the single or double quote in $loc_date string
for example
$loc_query = 'SELECT * FROM TABLE WHERE date = "'.$loc_date.'"';
OR
$loc_query = "SELECT * FROM TABLE WHERE date = '".$loc_date."'";
Put "" into the where condition.
$loc_query = 'SELECT * FROM TABLE WHERE date = "'.$loc_date.'"';
Then execute the query. It should give your result.
You can try this one :)
//creates database connection
$connection=mysql_connect("localhost","root","");
//selects database connection
$database_select=mysql_select_db("your_database_name",$connection);
//put the entered date to loc_date variable
$loc_date='2017-12-06';
//select * date with 2017-12-06 on table_query but would display only 1 since theres a LIMIT
$query1=mysql_query("select * from table_query WHERE date='$loc_date' LIMIT 1");
while($loc_query=mysql_fetch_array($query1)){
echo $loc_query['date']; //prints the date - 2017-12-06 if present
}
I think you want to display the date which is '2017-12-06' on your table_query. But since you want to limit the record to be displayed to 1, so only one(1) date which is 2017-12-06 would be displayed on that code. If theres more than 1 record(date:2017-12-06) saved on that table, you can remove the "LIMIT 1" on mysql query so that it will display all.
I am trying to do, what I assume is, an easy task of adding days to a date.
I have a date stored in a MySQL table, in a column called meta_date, with the type of DATE (A date, supported range is 1000-01-01 to 9999-12-31)
I retrieve this date from the database as follows:
$thisId = 1;
$dateQuery = mysqli_query($connection, "SELECT * FROM `sometable` WHERE `id` = '$thisId'");
$fetchDate = mysqli_fetch_assoc($dateQuery);
$theDate = $fetchDate['meta_date'];
Now I add a number of days to this date.
$newDate = date("Y-m-d", strtotime($theDate . " + 7 days"));
Next I put it back inside the database with an UPDATE query.
$editDate = mysqli_query($connection, "UPDATE `sometable` SET `meta_date` = '$newDate' WHERE `id` = '$thisId'");
However the date always returns as 0000-00-00 after the update.
Am I missing something here to do with the way the date is handled in PHP?
edit: The data I first retrieve from the database (into $theDate) is "2016-11-30".
You can use Mysql's built in function DATE_ADD()
Syntext
DATE_ADD(date,INTERVAL expr type) Where date is a valid date expression and expr is the number of interval you want to add.
For your case
UPDATE sometable
SET `meta_date` = DATE_ADD(`meta_date` , INTERVAL 7 DAY)
WHERE `id` = '$thisId';
I have a field on my website, which updates a value in my mysql database. I want to make it so the user can only update the value every 3 days. How would I go about doing this?
Here is the code that I have written so far:
<?php
if(isset($_POST['Update'])){
$UpdateHWID = $_POST['HWID'];
$sql = $con->query("UPDATE users SET HWID = '{$UpdateHWID}' where UserID = $User");
echo "HWID Updated Successfully!";
}
?>
Use a last updated field in mysql (date and time of last update), and check it before making the update. If satisfies your condition then commit the update and also update that time field, if not show error to the user.
Create new row into db (last_update) type= data
//return to sql last_update (select db ...)
$current_Data = date ('Y-m-d');
$current_Data_time = strtotime ($current_Data); //convert data to time
$last_update_p3 = strtotime ("+3day", strtotime($last_update));
$last_update_p3 = strtotime ($last_update_p3);//convert data to time
if($current_Data_time <=$last_update_p3)
{
$sql = $con->query("UPDATE users SET HWID = '{$UpdateHWID}' , last_update='{$current_Data}' where UserID = $User");
//update last data with current date
}
else
{
//It has not gone three days
}
According to Pinx0, if you add a new column to your users table which contains the date of the last update, then you can create a condition. For example:
ALTER TABLE `users`
ADD `lastUpdated` DATE NOT NULL;
Now you can add a condition to your existing query something like this:
UPDATE `users`
SET `HWID` = '{$UpdateHWID}',
`lastUpdated` = NOW()
WHERE `UserID` = $User AND 2 < DATEDIFF(CURDATE(),`lastUpdated`);
You can easily do that, using the DATEDIFF method.
It will be my first comment. so, Let me know if I have written
something incorrectly here.
$currentDate = date('Y-m-d');
$query = "SELECT DATEDIFF($currentDate,*Last_update_date_row_name*) as diff FROM *TABLE_NAME* WHERE user_id=Current_User_Id";
$result = mysqli_query($conn,$query);
if($result!=false){
//compare the diff with your desire number
// then you can hide or disable the button or show error
}
Would be grateful for some help!
Database Tables are set up like this:
id(varchar),
temp(varchar),
humi(varchar),
time(varchar)
Then I thought the user to input the ID, start date and end date.
The problem is how the string in the Time column is formatted, example: 18/03/14: 21:52:36
The user should not have to enter the time, just the date.
I thought it would be possible to do in a similar way:
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND time BETWEEN '$start%' AND '$stop%'");
But it did not work.
Is it possible to do this with a sql query when the date is stored in such a way?
Regards
. Anders
Edit:
It did not work, probably because I'm doing wrong though = /
If I do this:
$start= "13/02/14 : 12:17:34";
$stop = "13/02/14 : 12:36:18";
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND tid BETWEEN '$start' AND '$stop'");
..the data will appear as expected
But when I try to to use str_to_date () ,it did not work as I thought, or it did not come out any data at all.
$start= "13/02/14";
$stop = "10/02/14";
$id = "3E000004C6DB8D28";
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND tid BETWEEN str_to_date('$start%', '%d/%m/%Y') AND str_to_date('$stop%', '%d/%m/%Y')");
edit2:
Do not really know what I was doing weird the first time, but now it works with this code:
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND tid BETWEEN '$start' AND '$stop'");
You need to use str_to_date():
WHERE id = '$id' AND
tid BETWEEN str_to_date('$start%', '%d/%m/%Y') AND str_to_date('$stop%', '%d/%m/%Y')
Obviously, you can also do this in the application before inserting the values into the query. If so, convert the values to 'YYYY-MM-DD' format.