Alex Alex - 5 months ago 15
SQL Question

How to check if id already exists in mysql with php

I would like to check if mycode_sn exists in database before anything.
If mycode_sn doesn't exist, do nothing.

This is function:

function input_me_by_mycode($input_type, $input_id, $total_number)
{
if ($input_type == 'mycode') {
$my_info = $this->db->get_where('mytable', array(
'mycode_sn' => $input_id
))->row();

}

echo '<tr id="entry_row_' . $total_number . '">;

}


I have tried in several ways, but obviously I do not know that well.

Now in the case that doesn't exist I get message:


A PHP Error was encountered Severity: Notice Message: Trying to get
property of non-object

Answer

Try this

public function input_me_by_mycode($input_type, $input_id, $total_number)
{
    if ($input_type == 'mycode') {
        $query = $this->db->query("SELECT * FROM mytable WHERE mycode_sn = '$input_id' ");
        $result = $query->result_array();

        if (!empty($result)) {
            echo "ID Exist. ID is : ".$result[0]['mycode_sn'];
        } else {
            echo "ID doesn't Exist.";
        }

    } 
    else {
        echo "Input Type is not equesl to 'mycode' ";
    }   
}
Comments