0

I have this code for inserting prepared values into MySQL with PDO, but am looking for a way to tidy it up with a loop, to avoid repetition:

// PDO connect
    $pdo = new PDO($sqldsn, $sqluser, $sqlpass, $sqlopt);

    // Prepare query
    $stm = $pdo->prepare("$query");

    // Cycle through additional values
    foreach(func_get_args() as $arg) {
    $sqlarray[] = $arg;
    }

    // Execute query with values
    $count = count($sqlarray);

    if ($count == "2") { $stm->execute([$sqlarray[1]]); } 
    if ($count == "3") { $stm->execute([$sqlarray[1], $sqlarray[2]]); }
    if ($count == "4") { $stm->execute([$sqlarray[1], $sqlarray[2], $sqlarray[3] ]); }
    if ($count == "5") { $stm->execute([$sqlarray[1], $sqlarray[2], $sqlarray[3], $sqlarray[4] ]); } 
    if ($count == "6") { $stm->execute([$sqlarray[1], $sqlarray[2], $sqlarray[3], $sqlarray[4], $sqlarray[5] ]); }
    if ($count == "7") { $stm->execute([$sqlarray[1], $sqlarray[2], $sqlarray[3], $sqlarray[4], $sqlarray[5], $sqlarray[6] ]); }


    // Fetch all
    $result = $stm->fetchAll();

    // Return result    
    return $result;

I've tried looping like this, but I don't get any results (other than the number of array entries):

    for ($i = 1; $i<=$count; $i++){
        echo $i;
        $stm->execute([$sqlarray[$i]]);
    }

I also tried to construct the sections using eval(), but understand that this isn't a safe idea for security reasons.

I just need it to cycle through as per my code, but without having to specify each number of possible prepared ? values. Can anyone help? Sorry for the newb question, but I'm currently learning PHP.

2 Answers 2

3

You don't really need any loops at all.

You should be able to get the results you want with just a few lines of code;

$pdo = new PDO($sqldsn, $sqluser, $sqlpass, $sqlopt);

// Prepare query
$stm = $pdo->prepare($query);

$arr = array_values(func_get_args()); //gets rid of your foreach loop
array_shift($arr); //gets rid of first element of array

$stm->execute($arr);

return $stm->fetchAll();
Sign up to request clarification or add additional context in comments.

1 Comment

Great, that works perfectly, and with succinct code! Many thanks! :)
0

You can just remove the first element with array_shift:

// PDO connect
    $pdo = new PDO($sqldsn, $sqluser, $sqlpass, $sqlopt);

    // Prepare query
    $stm = $pdo->prepare("$query");

    // Cycle through additional values
    foreach(func_get_args() as $arg) {
    $sqlarray[] = $arg;
    }

    // Execute query with values
    $arr   = $sqlarray;  // Make a copy
    $first = array_shift($arr);  // Remove the first element
    $stm->execute($arr);  // Execute query with resulting array

    // Fetch all
    $result = $stm->fetchAll();

    // Return result    
    return $result;

3 Comments

Be careful using array_shift() with large arrays as each time an element is added or removed to the start of an array, the process for re-indexing all the other keys isn't efficient on larger arrays.
Thanks for your help! That works, but grumpy's answer avoids the loop altogether. :)
you're welcome! I had simply tried to address the repetition. Didn't realize you wanted to remove the foreach loop as well. But yes, @grumpy's solution is more elegant

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.