If i have ZKTime machine to register the attendance of the employees .
Sometimes this machine insert bulk of transactions in sql server db with wrong later date like
8-2103 instead of 11-2016
What are the possible causes of this problem and how to restore to the right date if i can't detect the problem ?
I've looked at the vendor link you supplied and it does not help in this case. I'm afraid we won't be able to answer this due to items outside of SQL Server. I believe you will need to contact Vendor Support for this.
The questions you will need to find out are:
How does the time machine calculate the CheckTime data?
How does the time machine store the CheckTime data?
How does the machine create the file to export to SQL Server?
This appears to be either an issue with how the system records the CheckTime data or in how it either exports / writes the data to SQL server.
As far as correcting the issue a basic update statement will fix it, but since there are different dates you will need to write a unique update for each case.
One possible solution is to make use of a Trigger to validate the date and update the date accordingly. Assuming the table has the Primary Key as id, if a newly inserted row has a date beyond today, it can be reset to the current datetime since employees' attendance record can't be in future.
CREATE TRIGGER CorrectTheDate on Config
FOR INSERT
AS
DECLARE #CT DateTime
DECLARE #id int
SELECT #CT = i.CheckTime FROM inserted i;
SELECT #id= i.id FROM inserted i;
if(#CT >= DATEADD(dd,1,getdate()))
UPDATE MyTable SET CheckTime=getdate() WHERE id=#id
GO
Related
I needed to simply add a new column to my DB table during development to accommodate a data change, however, my query when ran from my PHP script is not returning the column or data within said new column. My query is as straightforward as it gets SELECT * FROM time_table ORDER BY date DESC. It returns all previously existing columns from time_table, which leads me to believe there is a caching issue somewhere. I am using MAMP for local development, if that helps.
Thanks in advance.
Looks like the problem was a fault of my own.
Moral of the story: Do not forget to update your model(s) after making structure changes to the DB.
Is their anyway of checking whether data has been entered into the table and then displaying the updated data as soon as it is updated ??
What I can understand is that you are working for a Client-Server Message project.
You need to create a column for the timestamps in MySQL named anything like 'timestamp' and everytime you enter a entry in the table, for example a chat table, you need to insert the timestamp there in the same query.
In php you can use time() to get the current timestamp. You can set your timezone using this : date_default_timezone_set(), PHP timezones.
And save a variable which saves the last time retreived from the database. Send that with the query to the server.
Return the client all the messages based on the timestamp.
Use mysqli_error() to check, if this returns false you know the INSERT ran as you intended.
mysqli_query($conn,"INSERT INTO mytable (`columnone`,`columntwo`,`columnthree`)
VALUES ('$valueone','$valuetwo','$valuethree')")
or die(mysqli_error($db));
Now run a SELECT query to find the item you just inserted, and return it. Simple!
If you want all this to happen without reloading the page, use AJAX and return the result of the SELECT query upon successful submission.
If I understand you correctly I think you need to store a timestamp for every update and insert (or one for both). Just add a column to each table (I usually call it 'ts'). You can even make it update automatically: https://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
Edit: then you can obviously use that column to select the most recently updated row, see if anything's changed in X amount of time etc etc
I created a ticketing system that in its simplest form just records a user joining the queue, and prints out a ticket with the queue number.
When the user presses for a ticket, the following happens in the database
INSERT details INTO All_Transactions_Table
SELECT COUNT(*) as ticketNum FROM All_Transactions_Table WHERE date is TODAY
This serves me well in most cases. However, I recently started to see some duplicate ticket numbers. I cant seem to replicate the issue even after running the web service multiple times myself.
My guess of how it could happen is that in some scenarios the INSERT happened only AFTER the SELECT COUNT. But this is an InnoDB table and I am not using INSERT DELAYED. Does InnoDB have any of such implicit mechanisms?
I think your problem is that you have a race condition. Imagine that you have two people that come in to get tickets. Here's person one:
INSERT details INTO All_Transactions_Table
Then, before the SELECT COUNT(*) can happen, person two comes along and does:
INSERT details INTO All_Transactions_Table
Now both users get the same ticket number. This can be very hard to replicate using your existing code because it depends on the exact scheduling of threads withing MySQL which is totally beyond your control.
The best solution to this would be to use some kind of AUTO_INCREMENT column to provide the ticket number, but failing that, you can probably use transactions to achieve what you want:
START TRANSACTION
SELECT COUNT(*) + 1 as ticketNum FROM All_Transactions_Table WHERE date is TODAY FOR UPDATE
INSERT details INTO All_Transactions_Table
COMMIT
However, whether or not this works will depend on what transaction isolation level you have set, and it will not be very efficient.
I thought maybe there is MySQL command to pull up the last X records that were most recently modified. I know I could just put an extra field in that stores whenever a record is updated.. but I am trying to avoid that route if possible.
Any pointers? I just want to create a dynamic dashboard that allows the client to quickly work with the most recently worked on records..
Ex: "most recent users", "most recent projects", "most recent companies".. etc.
Rather than storing data in the database, you could easily use session or cookie storage.
Create a stack of most recent references with a fixed size and manipulate this when a record is worked on. Your dashboard can then use this stack to display the records.
Why are you trying to avoid storing it in the DB? too much extra data? or too much work? Did you know mysql can auto update the last-mod column?
ALTER TABLE mytable
ADD lastmodified TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
My application needs to poll a MySQL database for new rows. Every time new rows are added, they should be retrieved. I was thinking of creating a trigger to place references to new rows on a separate table. The original table has over 300,000 rows.
The application is built in PHP.
Some good answers, i think the question deserves a bounty.
For external applications I find using a TimeStamp column is a more robust method that is independent of auto id and other primary key issues
Add columns to the tables such as:
insertedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP
or to track inserts and updates
updatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
In the external application all you need to do is track the last timestamp when you did a poll. Then select from that timestamp forward on all the relevant tables. In large tables you may need to index the timestamp column
You can use the following statement to find out if a new record was inserted in the table:
select max(id) from table_name
replacing the name of primary key and table name in the above statement. Keep the max(id) value in a temporary variable, and retrieve all new records between this and the last saved max(id) value. After fetching the new records, set max(id) value to the one you got from the query.
Create a PHP Daemon to monitor the MySQL Table File size, if size changes query for new records, if new records found run next process.
I think there is an active PEAR daemon you can easily configure to monitor the MySQL Table file size and kick off your script.
assuming you have an identify or some other data that always grow, you should keep track on your php application of the last id retrieved.
that'd work for most scenarios. Unless you are into the real time camp, I don't think you'd need any more than that.
I would do something like this. Of course, this is assuming that ID is an incrementing numerical ID.
And how you store your "current location" in the database is upto you.
<?
$idFile = 'lastID.dat';
if(is_file($idFile)){
$lastSelectedId = (int)file_get_contents($idFile);
} else {
$lastSelectedId = 0;
}
$res = mysql_query("select * from table_name where id > {$lastSelectedId}");
while($row = mysql_fetch_assoc($res)){
// Do something with the new rows
if($row['id']>$lastSelectedId){
$lastSelectedId = $row['id'];
}
}
file_put_contents($idFile,$lastSelectedId);
?>
I would concurr with TFD's answer about keeping track of a timestamp in an separate file/table and then fetching all rows newer than that. That's how I do it for a similar application.
Your application querying a single row table (or file) to see if a timestamp has changed from the local storage should not be much of a performance hit. Then, fetching new rows from the 300k row table based on timestamp should again be fine, assuming timestamp is properly indexed.
However, reading your question I was curious if Mysql triggers can do system calls, say a php script that would do some heavy lifting. Turns out they can by using the sys_exec() User-Defined Function. You could use this to do all sorts of processing by passing into it the inserted row data, essentially having an instant notification of inserts.
Finally, a word of caution about using triggers to call external applications.
One option might be to use an INSERT INTO SELECT statement. Taking from the suggestions using timestamps to pull the latest rows, you could do something like...
INSERT INTO t2 (
SELECT *
FROM t1
WHERE createdts > DATE_SUB(NOW(), INTERVAL 1 HOUR)
);
This would take all of the rows inserted in the previous hour and insert them in to table 2. You could have a script run this query and have it run every hour (or whatever interval you need).
This would drastically simplify your PHP script for pulling rows as you wouldn't need to iterate over any rows. It also gets rid of having to keep track of the last insert id.
The solution Fanis purposed also sounds like it could be interesting as well.
As a note, the select query in the above insert can but adjusted to only insert certain fields. If you only need certain fields, you would need to specify them in the insert like so...
INSERT INTO t2 (field1, field2) (
SELECT field1, field2
FROM t1
WHERE createdts > DATE_SUB(NOW(), INTERVAL 1 HOUR)
);