0

the following code works to grab a return value from a php run_instance script. However, the value returned ($instanceIds) is in an array format. I need to insert these values, along with the userName and serverName into an sql database in separate rows for each value that is contained within the $instanceIds array. However, where I am getting confused is that I don't know how to insert the $instanceIds into the database in a way so that the userName and serverName are inserted over and over again until the end of the array. Any help is greatly appreciated

<?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);

?>
2
  • you must iterate (loop) over the content of $result and issue an 'INSERT ...' statement for each value in the array Commented Mar 26, 2014 at 15:48
  • mysql_real_escape_string() is deprecated and you shouldn't use it. Switch to prepared statements. Commented Mar 26, 2014 at 15:51

2 Answers 2

2
foreach ($instanceIds as $instanceId) {
    $server_record = mysql_query("INSERT INTO server_tbl "
        . "(userName, serverName, serverId, isRunning) "
        . "VALUES ('$name', 'runtest', '$instanceId', 'X'");
}
5
  • this approach doesn't work. The code runs fine without the foreach in there, but as soon as the foreach is inserted it breaks and just displays a white screen. Commented Mar 26, 2014 at 16:10
  • Sorry, I put in too many concatenation symbols. I fixed my answer Commented Mar 26, 2014 at 16:18
  • It works, it is just inserting $instanceID as "Array" Any ideas? Commented Mar 28, 2014 at 1:00
  • nevermind. Figured it out. Thanks again for your help. Commented Mar 28, 2014 at 1:03
  • What was the problem? Maybe I should update my answer once more. Commented Mar 31, 2014 at 6:58
0
  1. You really, really should not use mysql_real_escape_string() or mysql_query() or any of the mysql functions. They are deprecated, insecure, and will be removed from PHP in the future, breaking your entire app. Please save yourself and your users a lot of headache by using mysqli or PDO instead.

  2. Use a foreach loop to iterate over the array.

     foreach ($instanceIds as $id){
         $sql = "INSERT INTO server_tbl (userName, serverName, serverId, isRunning) VALUES ('$name', 'runtest', '$id', 'X')";
    
         //do your insert here with the above SQL statement, depending on whether you use mysqli or PDO
    

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.