shantanuo
shantanuo

Reputation: 32306

array data to mysql

I need to parse the following code and process the resulting data.

foreach($job as $x=>$x_value)
  {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
  }

The above code is returning the following as expected.

Key=vca_id, Value=20130<br>Key=uuid, Value=3c87e0b3-cfa<br>Key=originate_time, Value=2013-03-15 14:30:18<br>

What I need to do is to put the values in mysql database. So the insert statement would look something like this...

insert into test.master_table (vca_id, uuid, originate_time) values ('20130', '3c87e0b3-cfa', '2013-03-15 14:30:18')

What is the correct way to save the array values to mysql database?

Upvotes: 0

Views: 87

Answers (3)

alwaysLearn
alwaysLearn

Reputation: 6950

You can try this

$temp_value_arr = array();
$query = "INSERT into test.master_table SET ";
foreach($job as $x=>$x_value)
{
   $query .= "$x = '$x_value',";
}

$query = rtrim($query, ',');
mysql_query($query);

Upvotes: 1

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

Well i will recommend implode

$keys = array();
$values = array();
foreach($job as $x => $x_value)
{
    $keys[] =   $x;
    $values[]   =   $x_value;
}

$query  =   'INSERT INTO test.master_table' . '('.implode(',',$keys) .') VALUES (' .implode(',',$values) . ')';

Upvotes: 1

Rajeev Vyas
Rajeev Vyas

Reputation: 182

<?php 
mysql_query("insert into test.master_table(vca_id, uuid, originate_time)values('".$job['vca_id']."','".$job['uuid']."','".$job['originate_time']."')");
?>

Upvotes: 1

Related Questions