Welcome Guest, Not a member yet? Register   Sign In
Insert and Update database checking
#2

(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)
{
 
   $this->db->insert('users'$data);
 
   return $this->db->query('SELECT LAST_INSERT_ID() AS REC_ID;')->row()->REC_ID;


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)
{
 
   return $this->db->query(
 
       'SELECT
            gifts.id AS id,
            gifts.name AS gift,
            gifts.price AS price,
            gifts.description AS description,
            gifts.url AS url,
            glists.name AS glist,
            glists.max_gifts AS glist_max_gifts,
            glists.max_price AS glist_max_price,
            u2.name AS owner
        FROM gifts
        INNER JOIN users ON gifts.for_user_id = users.id
        INNER JOIN glists ON glists.id = gifts.glist_id
        INNER JOIN users u2 ON glists.owner = u2.id
        WHERE users.id = ' 
$user_id ';')->result_array();


Alright, good luck!  Smile
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!
Reply


Messages In This Thread
RE: Insert and Update database checking - by RobertSF - 12-26-2015, 04:07 PM



Theme © iAndrew 2016 - Forum software by © MyBB