0

When I Use Query Builder Function in codeIgniter, then Error message not shown.

Here Is a Controller

<?php
class Controller extends CI_Controller
{
    public function demo()
    {
        $this->load->model("Model");

        $data["name"] = $this->input->post("text");
        $res = $this->Model->add_data($data);
        echo $res;
    }
}
?>

Here Is a Model

<?php
class Model extends CI_Model
{
    public function add_data($data)
    {
        if ( ! $this->db->insert("table_name",$data))
        {
            $error = $this->db->error();
            return $error["message"];
        }
    }
}
?>
G_real
  • 1,137
  • 1
  • 18
  • 28

2 Answers2

1

Try this in your model

$data = array(
    'title' => 'My title',
    'name' => 'My Name',
    'date' => 'My date'
);

$sql = $this->db->set($data)->get_compiled_insert('mytable');
echo $sql;
if (!$this->db->simple_query($sql)) {
    $error = $this->db->error(); // Has keys 'code' and 'message'
}
echo'<pre>';print_r($error);die;

INSERT INTO mytable (title, name, date) VALUES ('My title', 'My Name', 'My date')

Array
(
[code] => 1146
[message] => Table 'db.mytable' doesn't exist
)

M.Hemant
  • 2,345
  • 1
  • 9
  • 14
0

Put this in your model and display it :

In CodeIgniter 3 like @Viren Panchal said, use this : error();

In your example :

$this->db->db_debug = false; 
$res = $this->Model->add_data($data); 
if(!$res) { 
    $error = $this->db->error(); 
    echo $error; 
}
Fizik26
  • 753
  • 2
  • 10
  • 25
  • I put this code.But here I same problem faced.Have any example ? – A.Developer Feb 13 '19 at 15:57
  • `$this->db->db_debug = false; $res = $this->Model->add_data($data); if(!$res) { $error = $this->db->_error_message(); echo $error; }` – Fizik26 Feb 13 '19 at 16:02
  • 1
    These methods `$this->db->_error_message()` and `$this->db->_error_number()` have been removed in CodeIgniter version 3. You can use $this->db->error() instead. – G_real Feb 13 '19 at 17:37