2012-03-29 10 views
-1

Durchführung habe ich Code wie folgt:MySQL Fehler 1064, wenn eine SELECT-Abfrage

function search_keyword(){ 
      $keyword = trim($_POST['keyword']); 
      $search_explode = explode(" ", $keyword); 
      $x = 0; 

      $sql = " (SELECT name, id_global_info AS id, body AS body, tag AS tag ,info_type_id AS info_type, \"global_info\" AS mytable FROM global_info WHERE "; 
      foreach($search_explode as $each){ 
       $x++; 
       if($x == 1){ 
        $sql .= " name LIKE '%$each%' ";}       
       else { 

        $sql .= " AND name LIKE '%$each%' "; 
       } 
      } 

       $sql .= ") UNION ALL "; 

       $sql .= " (SELECT name, id_person AS id, surname AS body, info AS tag , location AS info_type, \"person\" AS mytable FROM person WHERE "; 
      foreach($search_explode as $each){ 
       $x++; 
       if($x == 1){ 
        $sql .= " name LIKE '%$each%' ";}       
       else { 

        $sql .= " AND name LIKE '%$each%' "; 
       } 
      } 

       $sql .= ") UNION ALL "; 

       $sql .= "(SELECT name, id_event AS id, body AS body, caffe_id AS tag , date AS info_type, \"event\" AS mytable FROM event WHERE "; 
      foreach($search_explode as $each){ 
       $x++; 
       if($x == 1){ 
        $sql .= " name LIKE '%$each%' ";}       
       else { 

        $sql .= " AND name LIKE '%$each%' "; 
       } 
      } 

      $sql .= ") UNION ALL "; 

       $sql .= "(SELECT name, id_caffe AS id, description AS body, adress AS tag, location_id AS info_type, \"caffe\" AS mytable FROM caffe WHERE "; 
      foreach($search_explode as $each){ 
       $x++; 
       if($x == 1){ 
        $sql .= " name LIKE '%$each%' ";}       
       else { 

        $sql .= " AND name LIKE '%$each%' "; 
       } 
      } 

      $sql .= ") "; 
      echo $sql; 
      $q = $this->db->query($sql); 
      return $q = $q->num_rows() == 0 ? FALSE : $q->result(); 
     } 

Als ich für exapmle suchen

"mali oglasi"

bekomme ich folgende Fehlermeldung:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND name LIKE '%mali%' AND name LIKE '%oglas%') UNION ALL ( SELECT name, id_e' at line 1

Dies ist MySQL-Abfrage, die es produziert:

(SELECT name, id_global_info AS id, body AS body, tag AS tag ,info_type_id AS info_type, "global_info" AS mytable FROM global_info WHERE name LIKE '%mali%' AND name LIKE '%oglas%') 
UNION ALL 
(SELECT name, id_person AS id, surname AS body, info AS tag , location AS info_type, "person" AS mytable FROM person WHERE AND name LIKE '%mali%' AND name LIKE '%oglas%') 
UNION ALL 
(SELECT name, id_event AS id, body AS body, caffe_id AS tag , date AS info_type, "event" AS mytable FROM event WHERE AND name LIKE '%mali%' AND name LIKE '%oglas%') 
UNION ALL 
(SELECT name, id_caffe AS id, description AS body, adress AS tag, location_id AS info_type, "caffe" AS mytable FROM caffe WHERE AND name LIKE '%mali%' AND name LIKE '%oglas%') 

Was scheint ein Fehler zu sein?

Antwort

1

Das Wichtigste zuerst: Vergiss nicht, deinen Eingabewert zu verlassen. Dies kann in Ihrem Fall entweder auf dem Anfangswert oder für jede Iteration der foreach-Schleife auf $each

// If your query() method calls mysql_query() 
$keyword = mysql_real_eascape_string(trim($_POST['keyword'])); 
// Or if query() is mysqli::query() 
$keyword = $this->db->real_escape_string(trim($_POST['keyword'])); 
// Or if this is Codeigniter's API 
$keyword = $this->db->escape_like_str(trim($_POST['keyword'])); 

Sie müssen $x am Start zurückgesetzt jede foreach Schleife erfolgen:

 // Reset $x to 0 before the start of each of your loops. 
     $x = 0; 
     foreach($search_explode as $each){ 
      $x++; 
      if($x == 1){ 
       $sql .= " name LIKE '%$each%' ";}       
      else { 

       $sql .= " AND name LIKE '%$each%' "; 
      } 
     } 

Hinweis: Es wird im Allgemeinen empfohlen, parametrisierte Abfragen zu verwenden, anstatt die Abfrage durch Verkettung und Interpolation zu erstellen. Codeigniter verwendet ? Platzhalter dafür.

+0

ich so, wie Sie vorgeschlagen haben sich geändert, aber ich immer noch gleichen Fehler – Sasha

+0

@Sasha Sorry - ich habe es falsch . Das eigentliche Problem hierbei ist, dass Sie den Wert von $ x auf jeder der foreach-Schleifen auf Null zurücksetzen müssen. –

+0

Das war das Problem. Vielen Dank :) – Sasha

0

Ihre Anfrage Fehler befinden sich in der zweiten, dritten und vierten SELECT-Anweisungen, wie Sie WHERE AND name lieber als WHERE name