以下代码可从php run_instance脚本中获取返回值。但是,返回的值($ instanceIds)为数组格式。对于$ instanceIds数组中包含的每个值,我需要将这些值以及userName和serverName插入到sql数据库的单独行中。但是,令我感到困惑的是,我不知道如何以某种方式将$ instanceIds插入数据库,以便将userName和serverName反复插入,直到数组末尾。任何帮助是极大的赞赏

<?php
session_start();
$name=mysql_real_escape_string($_SESSION['username']);

include 'mysql_connect.php';

// Select the database to use
mysql_select_db("database",$con);

// Describe the now-running instance to get the public URL
$result = $ec2Client->describeInstances(array(
    'InstanceIds' => $instanceIds,
));


$server_record = mysql_query("INSERT INTO server_tbl (userName, serverName, serverId, isRunning) VALUES ('$name', 'runtest', '$instanceIds', 'X'");

print_r($instanceIds);

?>

最佳答案

foreach ($instanceIds as $instanceId) {
    $server_record = mysql_query("INSERT INTO server_tbl "
        . "(userName, serverName, serverId, isRunning) "
        . "VALUES ('$name', 'runtest', '$instanceId', 'X'");
}

10-07 17:56