0

How can I import an multiple array into a mysql database with php ?

The database I have created has only one table (jobs) and 3 Fields (plz,ort,work) later more fields, I want to run an cron job several times a day to get the database actual. The array comes from a other database witch I would like to import in my database.

My array:

stdClass Object
(
    [Code] => 0
    [Message] => 
    [Data] => Array
        (
            [0] => Array
                (
                    [plz] => 12345
                    [ort] => Testort
                    [work] => Schlosser
                )
            [1] => Array
                (
                    [plz] => 12345
                    [ort] => Testort
                    [work] => Schlosser
                )
        )
)

4 Answers 4

0

Something like this will

<?php
$stdObject = new stdClass();
$stdObject->data = array(
    array(
        "plz" => "12345",
        "ort" => "Testort",
        "work" => "Schlosser",
    ),
    array(
        "plz" => "12345",
        "ort" => "Testort",
        "work" => "Schlosser",
    )
);


// sql statement to insert data
$sql = "INSERT INTO job (plz,ort,work) VALUES :values";

// empty array to keep each row insertion
$values = array();

// makes following peaces
// ('12345', 'Testort', 'work')
// and push them into $values array.
foreach ($stdObject->data as $job) {
    $value .= "('" . $job['plz'] . "', '"
        . $job['ort'] . "', '"
        . $job['work'] . "')";
    $values[] = $value;
}

// convert values array to string
$values_string = implode(', ', $values);

// sql injection safe insertion
$pdo->prepare($sql);
$pdo->execute(array(
    'values' => $values_string
));

Also for Pdo class usage you can see http://php.net/manual/en/book.pdo.php

I hope this helps.

12
  • THX, I have imported the first data :). But the last problem I have is my format of my array. With your way of writing it runs. But how do I get my array into "$stdObject->data"?? I have no idea. I post the hole script when it runs complete :)
    – andy19
    Feb 12, 2015 at 11:52
  • simply do this: $stdObject->data = array(); // your array here.
    – Lost Koder
    Feb 12, 2015 at 13:29
  • or if you want add a array to data do this: $stdObject->data[] = array(); // your row_array here.
    – Lost Koder
    Feb 12, 2015 at 13:32
  • it does not run with my array shown in my project, can you help me?
    – andy19
    Feb 12, 2015 at 17:18
  • now it is done :) thx to you. i have used the wrong array before
    – andy19
    Feb 13, 2015 at 8:17
0

You can use $str = serialize() to save into the Datatbase and then use unserialize($str) to get it back.

1
  • sorry, but I am a beginner in programming. can you explain it to me with a coded example? this would be very nice.
    – andy19
    Feb 11, 2015 at 17:12
0
$str = '';

foreach($data as $values){
    //because we all love foreaches
    $str .= "(";
    while(list($key, $value) = each($values)){
        if($key == 'work'){
            $str .= "'$value' ";
        } else {
            $str .= "'$value', ";
        }
    }
    $str .= ")";
}

$query = "INSERT INTO jobs VALUES" . $str . "";

echo $query; 

Yes, I know this is horribly inefficient.

0

the code below is running but only one field I can't import.

// and push them into $values array.
foreach ($myarray->Data as $job) {
    $value = "('"
        . $job['plz'] . "','"
        . $job['ort'] . "','"
        . $job['work'] . "')";
    $values[] = $value;
}

// convert values array to string
$values_string = implode(', ', $values);

// sql statement to insert data
$sql = "INSERT INTO jobs (plz,ort,work) VALUES $values_string";

There are mutch more fields but one of the contains such html Code. The user does copy from word in to there system.

<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal"><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: DE'><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif'><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif'><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif'> </P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal"><B style="mso-bidi-font-weight: normal"><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: DE'>Lorem ipsum dolor<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal"><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: DE'><o:p> </o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal"><SPAN style='FONT-SIZE: 10pt; FONT-FAMILY: "Arial",sans-serif; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: DE'>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</SPAN></P>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.