doubledee — 2013-10-10T12:39:55-04:00 — #1
Yep, you heard it right!
I need some help learning how to make my queries FAIL!!
In testing right now, and I need to make some queries fail in order to make sure my error-handling is working properly.
1.) What is the easiest way to make an UPDATE query fail?? (Multiple ways are even better.)
2.) What is the easiest way to make an INSERT query fail?? (Multiple ways are even better.)
The UPDATE query is driving me particularly made, because everything I tried last night would not break my Prepared Statements?!
davemaxwell — 2013-10-10T12:45:15-04:00 — #2
The only way to get an update statement to truly fail is to put invalid data in fields (alpha characters in numeric fields, nulls in non-nullable fields, etc.) You can also get errors if you try and use values in FK fields which don't correspond to a record on the foreign table - IF you have the FK constraints defined properly.
For inserts, you have the same possibilities as above, plus duplicate errors on primary key fields, and not providing values in non-null fields.
doubledee — 2013-10-10T22:52:59-04:00 — #3
If the field "photo_approved" is defined as a tinyint, then how in the world can this query run...
SET photo_approved = '8888', last_activity = now()
WHERE id = 57
...without an error?!
photo_approved = 127
And how can this query run...
SET photo_approved = 'xxx', last_activity = now()
WHERE id = 57
...without an error?!
photo_approved = 0
And more importantly, how in the world do I get mysqli_stmt_affected_rows() to error out and return -1 like the Manual says it should when there is an error??????? :wall:
Every UPDATE query I try and concoct to make it fail ends up running?!
spacephoenix — 2013-10-11T02:13:07-04:00 — #4
Another method to make a query give an error on purpose would be for example to spell UPDATE as UPDATEE
guido2004 — 2013-10-11T06:33:57-04:00 — #5
Update a non existing table?
Insert more values than there are columns in the table?
doubledee — 2013-10-11T16:27:43-04:00 — #6
Doing that means my code won't even run!
I am trying to get this...
mysqli_stmt_affected_rows($stmt2) == -1
Which means I need the UPDATE query to run, but fail while running due to an SQL error.
Typing gobbly-gook obviously will break any PHP code.
doubledee — 2013-10-11T16:30:19-04:00 — #7
That won't even run. Again, that is not what I'm testing for.
I would be curious to know why the examples I gave above yield the results they do?! :eek:
I don't see how you can UPDATE a not-null field with a NULL, or UPDATE an Integer field with Text?!
force — 2013-10-11T21:27:01-04:00 — #8
What type of failures are you looking for besides syntax errors or invalid data?
Also, just because there might be data submitted that you don't want doesn't mean it can't be added to a database. There's a difference between invalid types of data going into a database (like trying to insert text into a numeric database column) and not getting the values you want or expect (like 888888 instead of 8).
spacephoenix — 2013-10-12T03:06:00-04:00 — #9
You're only going to get -1 when a query fails to run because of an error. If the query runs then the output of that function with be 0 if nothing was affected or a number >= 1. From the manual:
An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records where updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed. -1 indicates that the query returned an error.
guido2004 — 2013-10-12T08:40:57-04:00 — #10
Writing a gobbly-gook query does not break any PHP code, since it isn't PHP. It should cause MySQL to return with an error. I thought that was what you wanted. I don't understand what you mean by "that won't even run".