Insert and Update database checking |
(12-26-2015, 12:14 PM)pb.sajjad Wrote: I'm using CI3 and its query builders. I want know if I insert (or update) some data into tables, should I check insert (or update) result (to sure about operation)? In fact, is there some situation that MySQL does not insert or update? What is these reasons? This may not be the best practice, but in general, I do not check for SQL errors. If my field names are correct and I've already validated the data, the circumstances under which an SQL update could fail are very rare, I imagine (someone pulling the plug on the server?). If you wish to check for errors, CodeIgniter has $this->db->error(), which is found here: https://www.codeigniter.com/user_guide/d...ing-errors But I think if the SQL engine encounters an error, it will bring the PHP script to a halt with an error message SQL generates. Actually handing errors in PHP involves try/catch. Here's a tutorial of unknown quality: http://www.dreamincode.net/forums/topic/...-in-php-5/ and here's the official PHP docs on it: http://php.net/manual/en/language.exceptions.php Now, if you would like to retrieve the autoincrement field value immediately after you insert a row, you can use SQL's LAST_INSERT_ID() function. I implement it this way. PHP Code: public function add_user($data) And I realize this is not part of your question, but personally, I don't use CodeIgniter's query builders. Why? Because you should learn SQL anyway, and also, some queries are too complex for the query builders anyway. It's very convenient to "black box" the database connection, but for all but the simplest of queries, I like to write my own queries out in SQL. You can execute any SQL statement, even if it returns no rows, via $this->db->query(). So, for example, in my model file, I would have methods like this. PHP Code: public function get_gifts_for($user_id) Alright, good luck!
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
|
Messages In This Thread |
Insert and Update database checking - by pb.sajjad - 12-26-2015, 12:14 PM
RE: Insert and Update database checking - by RobertSF - 12-26-2015, 04:07 PM
RE: Insert and Update database checking - by skunkbad - 12-26-2015, 04:28 PM
RE: Insert and Update database checking - by pmbaldha - 12-27-2015, 01:27 AM
RE: Insert and Update database checking - by pb.sajjad - 12-27-2015, 04:37 AM
RE: Insert and Update database checking - by pmbaldha - 12-27-2015, 05:20 AM
RE: Insert and Update database checking - by pb.sajjad - 12-27-2015, 10:09 AM
RE: Insert and Update database checking - by pmbaldha - 12-27-2015, 08:58 PM
RE: Insert and Update database checking - by pb.sajjad - 12-28-2015, 03:33 AM
RE: Insert and Update database checking - by Diederik - 12-28-2015, 01:13 AM
|