2016-06-26 20 views
-1

Ich möchte überprüfen, ob mycode_sn in der Datenbank vor allem existiert. Wenn mycode_sn nicht existiert, nichts tun.Wie zu überprüfen, ob ID bereits in MySQL mit PHP

Das ist Funktion:

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 . '">; 

    } 

ich auf verschiedene Weise versucht haben, aber natürlich weiß ich nicht so gut.

nun in dem Fall, dass ich nicht existiert bekommen Nachricht:

Ein PHP-Fehler aufgetreten Schwere: Hinweis Nachricht: Eigenschaft von nicht-Objekt

Antwort

0

dieses zu erhalten Versuchen Versuchen

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' "; 
    } 
} 
+0

Vielen Dank, funktioniert perfekt! – Alex

+0

@Alex glücklich zu helfen –

+0

@Alex dieser Code ist anfällig für SQL-Injektion –

1

Verwendung aktiver Datensatz num_rows() zur Überprüfung Aufzeichnungen

public function input_me_by_mycode($input_type, $input_id, $total_number) 
{ 
    if ($input_type == 'mycode') { 
     $this->db->where('mycode_sn',$input_id); 
     $query = $this->db->get('mytable'); 
     $result = $query->row(); 

     if ($query->num_rows() > 0) 
      echo "ID Exist. ID is : ".$result->mycode_sn; 
     } else { 
      echo "ID doesn't Exist."; 
     } 

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