How can I detect a create, update, delete query is successful in Codeigniter
In crm_user_model->update()
, return true
or false
depending on the output of CodeIgniter's update()
function:
if ($this->db->update('mytable', $mydata)) {
// Do some stuff
return true;
} else {
// Do some stuff
return false;
}
Or if you don't need to do anything else in your model, just do this:
return $this->db->update('mytable', $mydata);
How do I test if my MySQL update query was successful in CodeIgniter?
As commented, have you tried $this->db->affected_rows()
?
That will tell you how many rows were updated.
Codeigniter - update query executed successfully but $this- db- affected_rows() returns false
You may use transaction to check if the query executed successfully or not. After that you can check the $this->db->affected_rows() to see if the admin really updated the user value or not, and return message accordingly.
Updated Code:
$this->db->trans_start();
// $where contains the array for where clause
// $data contains the array of user's data which includes the updated value
$this->db
->where($where)
->update('users', $data);
$this->db->trans_complete();
if($this->db->trans_status() === FALSE)
{
$response = array(
'message' => "Unable to edit user",
'status' => false
);
}
else
{
if($this->db->affected_rows() > 0)
{
$response = array(
'message' => "User edited successfully",
'status' => true
);
}
else
{
$response = array(
'message' => "User edited successfully, but it looks like you haven't updated anything!",
'status' => true
);
}
}
return $response;
Check if db- update successful with Codeigniter when potentially no rows are updated
If I understand correctly, you can use transactions to ensure that there was no error with the update:
$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->update('table',$array);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
// generate an error... or use the log_message() function to log your error
}
Just remember this only validates that there were no SQL errors during all the queries inside the transaction.
Here's the link for more info Code Igniter Active Record Transaction
How to check a query in CodeIgniter 3.0.3 (delete,update,select,insert)?
From the Cogidignitor User Guide 3:
For Select:
The number of rows returned by the query. Note: In this example, $query is the variable that the query result object is assigned to:
$query = $this->db->query('SELECT * FROM my_table');
if($query->num_rows() > 0){
return true;
}
else{
return false;
}
For UPDATE & INSERT Statements:
$this->db->affected_rows()
Displays the number of affected rows, when doing “write” type queries
(insert, update, etc.).
Example:
$this->db->update('UPDATE statement');
$this->db->affected_rows();
$this->db->insert('INSERT statement');
$this->db->affected_rows();
Alternate for INSERT:
You can also use last insert id as like:
$this->db->insert('INSERT statement');
$id = $this->db->insert_id();
if($id > 0){
return true;
}
else{
return false;
}
For Delete:
DELETE always return the true in result if query executed else false, affected_rows will also return same:
$this->db->delete('DELETE statement');
if($this->db->affected_rows()){
return true;
}
else{
return false;
}
CodeIgniter check if query succeeded
Use $this->db->affected_rows()
why delete is successful, but the database is not updated delete?
The reason this is happening is that you're updating the record and not deleting it. This is how you'd write a delete query
(REFERENCE) -
Model
public function menu_delete($data)
{
$this->db->where('id_menu', $data['id_menu']);
$this->db->delete('menu_user');
// Produces:
// DELETE FROM menu_user
// WHERE id_menu = $data['id_menu']
}
If you want to change only one column
status
from 0
to 1
and vice-versa, @Deepak Singh 's answer should do the deed.
See if it helps you.
Related Topics
How to Locate the PHP.Ini File (Xampp)
How to Run a PHP Script Inside a HTML File
Export MySQL Data to Excel in PHP
How to Get the Sqlsrv Extension to Work With PHP, Since Mssql Is Deprecated
Calculate Age Based on Date of Birth
PHP Fatal Error: Class 'Pdo' Not Found
How to Compile a Blade Template from a String
Netbeans Shows "Waiting For Connection (Netbeans-Xdebug)"
How to Detect a Create, Update, Delete Query Is Successful in Codeigniter
When to Use Strtr VS Str_Replace
Get in Woocommerce Cart the Product Id of a Cart Item
How to Convert Many Statement MySQL to Laravel Eloquent
Storing and Displaying Unicode String (हिन्दी) Using PHP and MySQL