Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7191067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:39:01+00:00 2026-05-28T19:39:01+00:00

So I have a few for each loops, and I’m trying to echo out

  • 0

So I have a few for each loops, and I’m trying to echo out the value of $sql at the very end. I’m actually using it with PDO but the echo demonstrates the issue.

$values = "1,2";
$values = explode(',',$values);

$set1 = "2,4";
$set1 = explode(',',$set1);
$set2 = "3,2";
$set2 = explode(',',$set2);

foreach($set1 as $set1val){
  if ($set1val==2) {
    $sql = "sql one runs";//should run on 1st iteration since 2 will equal 2
    echo 'hi';//should echo on 1st iteration since 2 will equal 2
  }
}

foreach($set2 as $set2val){
  if ($set2val==2) {
    $sql = "sql two runs";//should run on 2nd iteration since 2 will equal 2
    echo 'bye';//should echo on 2nd iteration since 2 will equal 2
  }
}

foreach($values as $value){
  echo $sql;
  $stmt = $db->query($sql);
}

//The Result Output
'hi'  'bye'  'sql two runs'  'sql two runs' <-- doesn't echo 'sql one runs' 

//The Output Required
'hi'  'bye'  'sql one runs'  'sql two runs' <-- Works great, but not outputting this

How can I make it output the output required? Any idea how to do this so the correct statement is echoed/runs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:39:02+00:00Added an answer on May 28, 2026 at 7:39 pm

    It never echos sql one runs because the second foreach overwrites it with sql two runs. If you need to save the sql strings through the end of the script, you could store them in an array. It’s difficult to specify how as it’s not clear how $values is related to the SQL, i.e., which SQL should run with which $value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few DropDownLists on a webform each pointing to a SqlDataSource. Some
I have a few models: 'Article, Video, BlogPost, News, Commodity'. Each are in their
I have written a few modules of code in Access vba. Each code runs
I have a package with a few modules, each module has a class (or
I have an admin view which contains four foreign keys each with a few
I have created a tab control and few forms. Each form opens in a
HI I have a form with a few buttons. Each button runs a few
Been trying to get my head around while loops for the last few days
Lets imagine that I have videos and each video can have few tags (maximal
I have written a few benchmarks using Caliper . How can I run multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.