Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Embedded php loop inside SQL query?

  • 17-07-2005 04: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, Registered Users 2 Posts: 32,132 ✭✭✭✭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