0

I have a mysqli_result:

$stmt = $db->prepare("SELECT * FROM customer");
$stmt->execute();
$result = $stmt->get_result();

Is it possible to directly do an INSERT with this result? (Data needs to be transferred in another database) like $another_db->insert($result) or at least to convert the whole result Object to a simple Mysql Insert String without iterating over the result.

Dharman
  • 30,962
  • 25
  • 85
  • 135
simonheinrich
  • 91
  • 1
  • 8

1 Answers1

1

I do a workaround like this:

$stmt = $db->prepare("SELECT * FROM customer");
$stmt->execute();
$result = $stmt->get_result();
$newsql="";
while($row =  $result->fetch_object())
{
    $newsql = "INSERT INTO customer VALUES (";
    foreach($row as $key => $value)
    {
        $newsql .=  "'".mysqli_real_escape_string($db,$value)."',";
    }
    $newsql = substr($newsql,0,-1);
    $newsql .="); ";
}
// $newsql can be inserted.
Dharman
  • 30,962
  • 25
  • 85
  • 135
simonheinrich
  • 91
  • 1
  • 8