Welcome Guest, Not a member yet? Register   Sign In
Query Builder's insert method return type
#1

Hey there,

I'm trying to insert some data into a database using the Query Builder's insert method.

According to the documentation, the insert method returns a boolean value.
https://codeigniter.com/user_guide/datab...tml#insert

However, the function seems to return a database result object upon success. The source appears to confirm this: https://github.com/codeigniter4/CodeIgni....php#L2249

PHP Code:
$success $this->db->table('MyTable')
                    ->insert($dataTRUE);

if (
$success !== TRUE) {
  return FALSE;



Is this deviation between the documentation and the actual behavior intentional?
How can I check that the insert statement completed successfully? Can I be sure that the insert statement always returns false on failure?

I appreciate any input Smile
Reply
#2

Almost the same problem is with builder->delete()

In case of some "query synax error" in the builder (e.g. when we try builder->where("ID", NULL)) it does not return false but an array(?).

There is no possibility to check if builder->delete() executed sucessully because inside builder->delete() the method raise DatabaseException which is handed by framework and not propagate forward to my code.


BTW: I have some workaround for you (but for database errors, not builder syntax errors): use $db->error();
Reply
#3

(This post was last modified: 02-04-2021, 04:19 PM by InsiteFX.)

I just checked the system code and it does return false or it returns the result, for CodeIgniter 4.1.1.

So the documentation is wrong it does not return true but the result.

What version of CodeIgniter 4 are you running?

Your $success is returning the result not false.

If the query does not validate it returns false.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 02-04-2021, 11:21 PM by shuwar.)

(02-04-2021, 04:12 PM)InsiteFX Wrote: I just checked the system code and it does return false or it returns the result, for CodeIgniter 4.1.1.

In my case (delete()) this is not true.

I am using 4.1.1 & ENVIRONMENT=production (DBDebug=false)

PHP Code:
$this->db Database::connect();
$this->db->transBegin();
$this->builder $this->db->table("SOMETABLE");
$this->builder->where('ID'NULL);
$result $this->builder->delete(); 

$result constains:
Code:
CodeIgniter\Database\MySQLi\Result::__set_state(array(
  'connID' =>
  mysqli::__set_state(array(
    'affected_rows' => NULL,
    'client_info' => NULL,
    'client_version' => NULL,
    'connect_errno' => NULL,
    'connect_error' => NULL,
    'errno' => NULL,
    'error' => NULL,
    'error_list' => NULL,
    'field_count' => NULL,
    'host_info' => NULL,
    'info' => NULL,
    'insert_id' => NULL,
    'server_info' => NULL,
    'server_version' => NULL,
    'stat' => NULL,
    'sqlstate' => NULL,
    'protocol_version' => NULL,
    'thread_id' => NULL,
    'warning_count' => NULL,
  )),
  'resultID' => true,
  'resultArray' =>
  array (
  ),
  'resultObject' =>
  array (
  ),
  'customResultObject' =>
  array (
  ),
  'currentRow' => 0,
  'numRows' => NULL,
  'rowData' => NULL,
));



$this->db->error()
 contains:

Code:
array (
  'code' => 0,
  'message' => '',
);
Reply
#5

(This post was last modified: 02-05-2021, 12:46 PM by InsiteFX.)

The delete method returns:

throw's new DatabaseException or returns false if where is empty.

throw's new DatabaseException ON LIMIT FOE sqlITE3.

Final return is test mode returns $sql else the query.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

It is a bug in query() method.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB