Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Embedded php loop inside SQL query?

Options
  • 17-07-2005 4:35pm
    #1
    Closed Accounts Posts: 38


    I want to embed a foreach() loop inside my SQL query as follows:
    $query = "UPDATE $table
                    SET " . foreach ($_REQUEST as $field => $value) {
                                         echo ("$field='$value'" . ", ");
                                }
                                echo ("$last_key='$last_value'"); .  // popped from array to avoid trailing comma
                             "WHERE student_id = $student_id";
    

    This results in a completely blank page so there is an error in the above code somewhere (page displays properly when above code snippet is commented out), or is what I am trying to do impossible?

    The foreach() loop above works perfectly outside the UPDATE statement.


Comments

  • Registered Users Posts: 32,136 ✭✭✭✭is_that_so


    PixelPixie wrote:
    I want to embed a foreach() loop inside my SQL query as follows:
    $query = "UPDATE $table
                    SET " . foreach ($_REQUEST as $field => $value) {
                                         echo ("$field='$value'" . ", ");
                                }
                                echo ("$last_key='$last_value'"); .  // popped from array to avoid trailing comma
                             "WHERE student_id = $student_id";
    

    This results in a completely blank page so there is an error in the above code somewhere (page displays properly when above code snippet is commented out), or is what I am trying to do impossible?

    The foreach() loop above works perfectly outside the UPDATE statement.

    My first question is why ?

    The . notation used here expects either a variable($var .) or a string (eg " string"). You have given it neither.

    My suggestion is to populate the rest of the UPDATE outside this and then append it. Then just execute it . :)
    The loop is valid code and works but based on what you have posted you already have the key=> value terms so build it outside.


  • Closed Accounts Posts: 38 PixelPixie


    is_that_so wrote:
    My first question is why ?

    The . notation used here expects either a variable($var .) or a string (eg " string"). You have given it neither.

    Why? Because I want to read in variables extracted from key-value parirs in array to the SET part of the sql statement. Surely the output values of $field and $value count as strings?
    is_that_so wrote:
    My suggestion is to populate the rest of the UPDATE outside this and then append it. Then just execute it . :)
    The loop is valid code and works but based on what you have posted you already have the key=> value terms so build it outside.

    Thanks for the feedback. I'll do that.


Advertisement