Why Mysql Insert Query Returns False

Some days ago I face a problem;  which is my mysql insert query retun false. So I need to dubug the query and wanna to know which problem is here. This help me very much:-

trigger_error(mysql_error());

to debug the query.  I think sometime you need help with that.

$sql = "here is you mysql query";
mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);

How to Subtract two dates in php

$date_one = "2013-09-02 04:24:25";

$date_two = "2012-09-02 04:24:25";

$to_date = strtotime($date_one);
$from_date = strtotime($date_two);

$date_difference = $to_date- $from_date; // Get date difference between the two dates in number of seconds
$days = floor($date_difference / (24 * 60 * 60 )); // convert to days
echo($days); // print 365

CodeIgniter jQuery Ajax live search

View file –

<div class="something">
     <input name="search_data" id="search_data" type="text" onkeyup="ajaxSearch();">
     <div id="suggestions">
         <div id="autoSuggestionsList"></div>
     </div>
</div>

Javascript Code –

<script type="text/javascript">

function ajaxSearch()
{
    var input_data = $('#search_data').val();

    if (input_data.length === 0)
    {
        $('#suggestions').hide();
    }
    else
    {

        var post_data = {
            'search_data': input_data,
            '<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>'
            };

        $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>welcome/autocomplete/",
            data: post_data,
            success: function (data) {
                // return success
                if (data.length > 0) {
                    $('#suggestions').show();
                    $('#autoSuggestionsList').addClass('auto_list');
                    $('#autoSuggestionsList').html(data);
                }
            }
         });

     }
 }
</script>

Controller Function – [Welocome]

public function autocomplete()
{
     // load model
     $this->load->model('Welcome_model');

     $search_data = $this->input->post('search_data');

     $result = $this->Welcome_model->get_autocomplete($search_data);

     if (!empty($result))
     {
          foreach ($result as $row):
               echo "<li><a href='#'>" . $row->name . "</a></li>";
          endforeach;
     }
     else
     {
           echo "<li> <em> Not found ... </em> </li>";
     }
 }

Model Function – [Welcome_model]

SELECT id, name FROM `fruits`
public function get_autocomplete($search_data)
{
    $this->db->select('name, id');
    $this->db->like('name', $search_data);

    return $this->db->get('fruits', 10)->result();
}

A little bit styling –

input[type=text] {
    width: 200px;
    padding: 5px;
    margin: 5px 0;
    box-sizing: border-box;
}

#autoSuggestionsList > li {
    background: none repeat scroll 0 0 #F3F3F3;
    border-bottom: 1px solid #E3E3E3;
    list-style: none outside none;
    padding: 3px 15px 3px 15px;
    text-align: left;
}

#autoSuggestionsList > li a { color: #800000; }

.auto_list {
    border: 1px solid #E3E3E3;
    border-radius: 5px 5px 5px 5px;
    position: absolute;
}

 

You can download the whole CodeIgniter Code from the github repo.

 

If it doesn’t work or you face any error to implement the code, Please send me a mail (me@orionwebtech.net). I will try to respond you.