we5inelgr;11036101 wrote:
1. To tailer the resulting message to the user and
I understood that you specifically wanted to provide the user with different feedback. Otherwise you wouldn't have asked the question to begin with. I'm asking about the purpose for this. What do you intend for the user to do with this information when they do not ask for it?
we5inelgr;11036131 wrote:
However using this attribute will only return the number of found rows, instead of the number of affected (by the UPDATE) rows.
Which is the sensible thing to do. The reason it makes sense is that the communication with the db looks like this
Set these 5 rows to 'A'
5 rows updated!
And anything else is senseless…
Update these 5 rows!
Ok! 0 rows updated!
You are in effect talking about something entirely different - as you will see when we get to the "solution". In the last "5 row-example" above, you would not be talking about setting 5 rows to 'A'. You would be talking about setting 0 rows to 'A'. And suddenly the standard (non-mysql) response makes sense again
Set these 0 rows to 'A'
Ok! 0 rows updated!
we5inelgr;11036131 wrote:
In the case of the user trying to update a record with the same information (yes, it happens
Of course it happens. Example with a database: I want to know that some test account of mine is not a paying member to verify that I'm denied access to certain features and that I see ads. I tell the db to set the membership expiration date for such a test account to [some previous date] and the admin page tells me: nothing saved? 0 users updated? 0 users updated (but 1 where found)? Hell if I care. Just let me know: user updated! This is the only answer that tells me that it did find the user I was talking about and that its membership has expired. Which non-incidentally happens to be why I told the system to do in the first place.
Another example. I have a text editor showing a text file. I hit save and it tells me…
Saved!
-> I'm happy
Why? Because if I tell my text editor to save the file, I do not care wether its contents have changed or not. I care about the current contents being there the next time I open that file. Hitting save and getting information back such as
"Nothing saved!
You already saved these exact contents, so we decided to not save it!"
has nothing to do with what I told it to do. If I wanted to know if the current version was different from what I had on disk, I would tell it to compare it to whatever was on disk.
In real life it may actually be that the text editor does not tell me "saved". Success is expected and therefor implied. But it would tell me about failure. And the only time I'm interested about hearing what was not done is when it wasn't done due to a failure.
we5inelgr;11036131 wrote:
. trying to code for this situation), the rowCount() with this attribute will return 1 as will a record that is updated with new information.
Because the user said: Save this record with this particular info!
Sensible response: I did what you asked! 1 row saved!
we5inelgr;11036131 wrote:
It appears that if one is using PDO/preparted statements, that there isn't a way to differentiate between:
1. Found record, no update due to same data
2. Found record, update
Yes there is. It's called "SQL"
Let this be your initial update query which you are not happy with
UPDATE t1
SET f1=v1, f2=v2, …, fn = vn
WHERE c1=w1 AND c2=w2
And let's have a look at what this query returns
SELECT f1, f2, …, fn
FROM t1
WHERE c1=w1 AND c2=w2
They have the same rows, and personally I'd expect the first to report the same number of updated rows (with no pre-update trigger modifications) as the SELECT statement finds - They are after all using the same selection criteria.
Now, assuming I'd want to know what rows will have no changes to data… well obviously this can be determined in advance, and therefor can be communicated to the db.
SELECT f1, f2, …, fn
FROM t1
WHERE c1=w1 AND c2=w2
AND f1=v1 AND f2=v2 AND … AND fn = vn
will select, from all rows matching the initial selection criteria, that already contain the data we are updating it with.
This can obviously also be used as the criteria for an update, and by providing this criteria, the answer that comes back answers your question
we5inelgr;11036131 wrote:
1. Found record, no update due to same data
What you are actually talking about, in natural language, is
we5inelgr;11036131 wrote:
records matching whatever I was talking about to begin with but only if they also already contain this set of data
So, the update that matches your question 1.
UPDATE t1
SET f1=v1, f2=v2, …, fn = vn
WHERE c1=w1 AND c2=w2
AND f1=v1 AND f2=v2 AND … AND fn = vn
Which leaves
we5inelgr;11036131 wrote:
2. Found record, update
What you are NOT saying here, but what is implicitly implied by following your posts in this thread, is in reality
we5inelgr;11036131 wrote:
All of the records specified by whatever criteria you initially were taking about, THAT ALSO do not already contain the set of data now provided
And as soon as you actually include your implied meaning to the database, it will happily provide an answer to it.
Back to the select query to see what data we will be operating on…
SELECT f1, f2, …, fn
FROM t1
WHERE c1=w1 AND c2=w2
AND NOT (f1=v1 AND f2=v2 AND … AND fn = vn)
In case the last where criteria isn't readily apparent, do note that because it is enough that one single field/value pair does not match for the data to have changed, the criteria could have been written
WHERE c1=w1 AND c2=w2
AND (f1 != v1 OR f2 !=v2 OR … OR fn != vn)
Which using boolean algebra leads to the first expression.
we5inelgr;11036131 wrote:
like there is with mysqli_info() ? 🙁
Because it's nonsense. If you ask the db other questions than you think you are asking it, you will not understand its answers. If you ask the db the proper questions, the answers you get will make sense. Assuming that you let mysql know that you want sensible answers: PDO::MYSQL_ATTR_FOUND_ROWS