0

I'm trying to insert array into database. Why don't this work?

$array_zone = array();
$array_data = array();
while($row = mysql_fetch_array($keputusan1)){
$array_zone[] = $row['zone'];
$array_data[] = $row['data'];
}

echo "<pre>";
print_r($array_zone);
echo "<br>";
print_r($array_data);
echo "</pre>";

$list_zone = implode(",", $array_zone);
$list_data = implode(",", $array_data);

for($i = 0; $i < 4; $i++)
{
mysql_query("INSERT INTO `db`.`table1` (`id`, `domain`) VALUES ('$list_zone', '$list_data')");
}

Array output before implode:

Array
(
    [0] => 270
    [1] => 270
    [2] => 255
    [3] => 255
)

Array
(
    [0] => ok.com.
    [1] => lo.com.
    [2] => i.com.
    [3] => k.com.
)

Result I'm getting: phpmyadmin

0 key should go with data in 0 key and 1 key should go with data in 1 key and so on... Help me please. Thank you.

3
  • 2
    You seem to be using $list_zone and $list_data instead of $array_zone and $array_data Commented Feb 19, 2012 at 9:33
  • @niomaster: I update my question. It's still not working. Commented Feb 19, 2012 at 9:36
  • 1
    @sg552 did you see my answer. Its what you need. Commented Feb 19, 2012 at 9:51

4 Answers 4

4

You can use INSERT ... SELECT. The result returned in $keputusan1 is a SELECT query. And it certainly returns zone and data columns. Say the query is SELECT zone, data, columns ... FROM tbl1.

If you use INSERT ... SELECT the new query would be

INSERT INTO `db`.`table1` (`id`, `domain`) SELECT zone, data FROM tbl1";

This will SELECT first then INSERT it in one shot.

The problems with your code are

  1. 4 SQL statements. It should be 1.
  2. You should use $array_zone and $array_data instead of $list_zone and $list_data if for loop is used. But it should not be as of step 1.
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. This solve it and much efficient. I learned new thing today. Thank you :)
2

It seems to me, that you just forgot to use the loop-index ($i) within the loop to access the arrays elements:

#!/usr/bin/php
<?php
 $array_zone = array('a','b','c','d');
 $array_data = array('1','2','3','4');

print_r($array_zone);
print_r($array_data);

for($i = 0; $i < count($array_zone); $i++)
{
  echo("INSERT INTO `db`.`table1` (`id`, `domain`) VALUES ('$array_zone[$i]', '$array_data[$i]')\n");
}
?>

Now replace that echo with your mysql_query call and remove the '\n' at the end...

1 Comment

Oh I can only choose 1 correct answer. You answer did solve my problem but other user gave me other approach that is much efficient. Anyway I really appreciate your help. Thank you :)
1

Your list_zone (really array_zone) is enclosed in a single set of single quotes, so you're passing one long string. And you're doing it four times. Pass it one pair of values at a time.

Comments

0

Are you sure you want to do this in that way? Probably a good idea to try the serialize() function instead of the implode. http://php.net/manual/en/function.serialize.php

Maybe an even better idea to use JSON for this task. http://php.net/manual/en/function.json-encode.php

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.