1

I have an array called $output which contains:

Array
(
    [0] => Array
        (
            [0] => J Arora
            [1] =>  India
        )

    [1] => Array
        (
            [0] => J Ramuj
            [2] =>  Russia
        )

    [2] => Array
        (
            [1] =>  KJ Tris
            [2] =>  Germany
        )

)

How can i insert these data into mysql database table like these

---------------
name   | country
-------|---------
J Arora|India
-------|--------
J Ramuj|Russia
-------|--------
KJ Tris|Germany
-------|--------

I am not able to fetch these values separately from given array as their index are not in sequence.

3
  • simply you can use foreach loop to insert the same ? do want the insert query ? Commented Dec 23, 2016 at 10:54
  • What database abstraction layer are you using? Commented Dec 23, 2016 at 10:55
  • How do you know, what is the name and what is the country? Commented Dec 23, 2016 at 11:33

5 Answers 5

5

Just loop it with a foreach, and use PHP native functions like current() and end() to get your elements. Given that you just have two elements in your array at all times, this should work

foreach ($output as $v) {
    echo current($v); // Name
    echo end($v); // Country
}

Adapt this to build your query and execute it inside the loop. Inside the loop you could do

foreach ($output as $v) {
    $sql = "INSERT INTO table (`name`, `country`) VALUES ('".current($v)."', '".end($v)."')";
    // TODO: Execute query
}

You should also note that any query using variables should be using prepared statements with placeholders. Both MySQLi and PDO supports this.


Using prepared statements

Build the query before, and execute it as you loop it with the appropriate variables bound.

Example with PDO:

$stmt = $pdo->prepare("INSERT INTO table (`name`, `country`) VALUES (:name, :country)");
foreach ($output as $v) {
    $stmt->execute(array("name" => current($v), "country" => end($v));
}

Example with MySQLi:

$stmt = $mysqli->prepare("INSERT INTO table (`name`, `country`) VALUES (?, ?)");
foreach ($output as $v) {
    $name = current($v);
    $country = end($v);
    $stmt->bind_param("ss", $name, $country);
    $stmt->execute();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I was not aware of these functions.
The best approach is of course to use prepared statements - we're dealing with variables here. Updated my answer to reflect that with examples for both MySQLi and PDO.
Yes. I have used PS only.
0
$sql = "INSERT INTO tablename(name,country) VALUES";

for($i=0;$i<sizeof($myarray);$i++){
  $sql.="(".$myarray[0].",".$myarray[1].")";
}

now execute $sql

Comments

0
for ($a=0; $a<count($array); $i++) {
    $dataArray=array_values($array[$i]);
    for ($j=0; $j<count($dataArray); $j++) {

        $name=$dataArray[0];
        $country=$dataArray[1];
        $insert_query="insert into tablename set name='$name', country='$country'";
        $res_query=mysql_query($insert_query);
    }
}

Hope this will works for you.

Comments

0

Let's call your array user_details

foreach($user_detail as $single_user){
       $i = 0;
       foreach($single_user as $user){
           if($i==0){
             $name = $user;
           } else {
            $country = $user;
           } $i++;
       }
     $sql_query = INSERT into 'Table'($name, $country);
     //Execute this query as per mysqli or or mysql

}

//Considering that first value is always the name and 2nd values is always a country name

2 Comments

I am not able to fetch these values separately from given array as their index are not in sequence.
index of arrays are not in sequence. this will not work.
0

Might be this can help. Its not tested.

$query = "INSERT into 'your table name' ('name', 'country') VALUES ";
foreach($yourArray as $data) {
    foreach($data as $d) {
       $query .= "('.$d[0].','.$d[1]),";
   }
}

$query = substr($query, 0, -1); // To remove last comma from query string

Then you can execute this query.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.