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

  • SEARCH
  • Home
  • 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 8919245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:58:13+00:00 2026-06-15T05:58:13+00:00

I was told that it is a bad practice to use a query (select)

  • 0

I was told that it is a bad practice to use a query (select) within a loop because it slows down server performance.

I have an array such as

Array ( [1] => Los Angeles )
Array ( [2] =>New York)
Array ( [3] => Chicago )

These are just 3 indexes. The array I’m using does not have a constant size, so sometimes it can contain as many as 20 indexes.

Right now, what I’m doing is (this is not all of the code, but the basic idea)

  1. For loop
  2. query the server and select all people’s names who live in “Los Angeles”
  3. Print the names out

Output will look like this:

Los Angeles
      Michael Stern
      David Bloomer
      William Rod

New York
      Kary Mills

Chicago
      Henry Davidson
      Ellie Spears

I know that’s a really inefficient method because it could be a lot of queries as the table gets larger later on.

So my question is, is there a better, more efficient way to SELECT information based on the stuff inside an array that can be whatever size?

  • 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-06-15T05:58:15+00:00Added an answer on June 15, 2026 at 5:58 am

    To further add to MrCodes answer, if you start with an array:-

    $Cities = array(1=>'Los Angeles', 2=>'New York', 3=>'Chicago');
    $query = "SELECT town, personname FROM people WHERE town IN('".implode("','", $Cities)."') ORDER BY town";
    if ($sql = $mysqliconnection->prepare($query)) 
    {
        $sql->execute();
        $result = $sql->get_result();
        $PrevCity = '';
        while ($row = $result->fetch_assoc()) 
        {
            if ($row['town'] != $PrevCity)
            {
                echo $row['town']."<br />";
                $PrevCity = $row['town'];
            }
            echo $row['personname']."<br />";
        }
    }
    

    As a database design issue, you probably should have the town names in a separate table and the table for the person contains the id of the town rather than the actual town name (makes validation easier, faster and with the validation less likely to miss records because someone has mistyped their home town)

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

Sidebar

Related Questions

My office colleague told me today that is bad practice to use properties in
I have previously been told that I should always use Randomize() before I use
I've been told at school that it's a bad practice to modify the index
We have been told that we have to use a piece of software called
So I've always been told that it's bad practice to modify arguments to methods
I have been told recently that extending the Application Class in order to use
My superior once told me that a bad way of calling a web service
Someone told me that openGL is for graphic only, and that it's very bad
I was told that Respond.Redirect is an expensive process because it raises a ThreadAbortException.
I was told that the optimal way to program in C++ is to use

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.