2013-05-13 133 views
-1

我有以下表命名为“希腊语”插入合并数组数据到MySQL表

 ID -- greek -- greeklish 

我有以下码取文字,并创建2个数组:

 $str = "Κρίσιμη για το μέλλον των τμημάτων του βόλεϊ είναι η προγραμματισμένη για σήμερα συνεδρίαση του Δ.Σ. στον Ερασιτέχνη Παναθηναϊκό εάν κι εφόσον εμφανιστεί ο πρόεδρος Αχιλλέας Μακρόπουλος ο οποίος για περισσότερο από ένα μήνα δεν έχει επισκεφθεί τα γραφεία του συλλόγου προφανώς ενοχλημένος από τη συμπεριφορά ορισμένων παραγόντων εντός κι εκτός του συλλόγου Στις γυναίκες πρέπει να ληφθούν σημαντικές αποφάσεις για το μέλλον του τμήματος σχετικά με μεταγραφές ενώ μετά την απομάκρυνση του Βασίλη Βλαχονικολού από τα τμήματα υποδομής θα πρέπει να συζητηθεί και να αποφασιστεί η πρόσληψη νέου προπονητή που θα οδηγήσει την Εθνική Κορασίδων στα τελικά του Πανελληνίου πρωταθλήματος Φήμες πάντως λένε πως ο Μακρόπουλος δεν ήταν ενήμερος για την απομάκρυνση του Βλαχονικολού και κυρίως για τους λόγους που έγινε Αποφάσεις περιμένει και το ανδρικό τμήμα σχετικά με την επιβίωση του αν και τα πάντα είναι σε συνάρτηση με την απόφαση του ΑΣΕΑΔ σχετικά με την τιμωρία ή όχι του Ηρακλή Οι ημέρες είναι κρίσιμες για τον Παναθηναϊκό Την ώρα που όλες οι ομάδες αργά αλλά σταθερά προγραμματίζουν το μέλλον τους η διοίκηση του τριφυλλιού βρίσκεται αντιμέτωπη με μία σημαντική πρόκληση καθώς εάν χάσει κι άλλο πολύτιμο χρόνο δεν θα μπορέσει να αντιμετωπίσει τα προβλήματα αλλά και τον ανταγωνισμό"; 
     $str2 = "Krisimh gia to mellon twn tmhmatwn tou volei einai h programmatismenh gia shmera synedriash tou D.S. ston Erasitexnh Panathhnaiko ean ki efoson emfanistei o proedros Axilleas Makropoulos o opoios gia perissotero apo ena mhna den exei episkefthei ta grafeia tou syllogou profanws enoxlhmenos apo th symperifora orismenwn paragontwn entos ki ektos tou syllogou Stis gynaikes prepei na lhfthoyn shmantikes apofaseis gia to mellon tou tmhmatos sxetika me metagrafes enw meta thn apomakrynsh tou Vasilh Vlaxonikoloy apo ta tmhmata ypodomhs tha prepei na syzhththei kai na apofasistei h proslhpsh neou proponhth pou tha odhghsei thn Ethnikh Korasidwn sta telika tou Panellhniou prwtathlhmatos Fhmes pantws lene pws o Makropoulos den htan enhmeros gia thn apomakrynsh tou Vlaxonikoloy kai kyriws gia tous logous pou egine Apofaseis perimenei kai to andriko tmhma sxetika me thn epiviwsh tou an kai ta panta einai se synarthsh me thn apofash tou ASEAD sxetika me thn timwria h oxi tou Hraklh Oi hmeres einai krisimes gia ton Panathhnaiko Thn wra pou oles oi omades arga alla stathera programmatizoun to mellon tous h dioikhsh tou trifyllioy vrisketai antimetwph me mia shmantikh proklhsh kathws ean xasei ki allo polytimo xrono den tha boresei na antimetwpisei ta provlhmata alla kai ton antagwnismo"; 
     $array = explode(' ', $str); 
     $array2 = explode(' ', $str2); 

     function merge(){ 
      //check if there was at least one argument passed. 
      if(func_num_args() > 0){ 
       //get all the arguments 
       $args = func_get_args(); 
       //get the first argument 
       $array = array_shift($args); 
       //check if the first argument is not an array 
       //and if not turn it into one. 
       if(!is_array($array)) $array = array($array); 
       //loop through the rest of the arguments. 
       foreach($args as $array2){ 
        //check if the current argument from the loop 
        //is an array. 
        if(is_array($array2)){ 
         //if so then loop through each value. 
         foreach($array2 as $k=>$v){ 
          //check if that key already exists. 
          if(isset($array[$k])){ 
           //check if that value is already an array. 
           if(is_array($array[$k])){ 
            //if so then add the value to the end 
            //of the array. 
            $array[$k][] = $v; 
           } else { 
            //if not then make it one with the 
            //current value and the new value. 
            $array[$k] = array($array[$k], $v); 
           } 
          } else { 
           //if not exist then add it 
           $array[$k] = $v; 
          } 
         } 
        } else { 
         //if not an array then just add that value to 
         //the end of the array 
         $array[] = $array2; 
        } 
       } 
       //return our array. 
       return($array); 
      } 
      //return false if no values passed. 
      return(false); 
     } 


     $merged = merge($array, $array2); 

     var_dump($merged); 

这导致以下:

 array 
      0 => 
      array 
       0 => string 'Κρίσιμη' (length=14) 
       1 => string 'Krisimh' (length=7) 
      1 => 
      array 
       0 => string 'για' (length=6) 
       1 => string 'gia' (length=3) 
      2 => 
      array 
       0 => string 'το' (length=4) 
       1 => string 'to' (length=2) 
      3 => 
      array 
       0 => string 'μέλλον' (length=12) 
       1 => string 'mellon' (length=6) 
      4 => 
      array 
       0 => string 'των' (length=6) 
       1 => string 'twn' (length=3) 

我如何将所有这些值插入到上述的数据库表中?

感谢

+0

我不明白...... – worenga 2013-05-13 11:31:13

回答

3

使用PDO,你可以使用下面的代码:

//Open a PDO connection to the database and store the connection id on $dbh. 
//Adjust database name, username and password 
$dbh = new PDO('mysql:host=localhost;dbname=your_database_name', $user, $pass); 
//Prepare the statement 
$statement = $dbh->prepare("insert into greek(greek, greeklish) values(?, ?)"); 
//Loop over the array and execute the queries 
foreach($merged as $values){ 
    //Note $values should be an array   
    $statement->execute($values); 
} 
+0

优秀!它完成了这项工作! – 2013-05-13 11:39:06

+0

最后一个问题,如果表中存在一个短语,我该如何避免重复? – 2013-05-13 11:40:54

+1

如果您为该字段定义了“唯一”索引,则使用“插入忽略”将跳过插入重复值。在这里检查http://dev.mysql.com/doc/refman/5.1/en/insert.html – Ander2 2013-05-13 11:44:12