dongqu2863 2013-05-13 11:23
浏览 55
已采纳

将合并的数组数据插入mysql表

i have the following table named 'greek'

     ID --  greek -- greeklish

I have the following code that takes text and creates 2 arrays:

        $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);

this results in the following:

        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)

and so on

how i can insert all these values to the database table described above?

thanks

  • 写回答

1条回答 默认 最新

  • dougou6213 2013-05-13 11:35
    关注

    Using PDO you could use the following code:

    //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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单