简体   繁体   English

插入 mysqli_result Object

[英]Insert a mysqli_result Object

I have a mysqli_result:我有一个 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?是否可以直接使用此结果进行 INSERT? (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. (数据需要转移到另一个数据库中),如$another_db->insert($result)或至少将整个结果 Object 转换为简单的 Mysql 插入字符串而不迭代结果。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM