6

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?

2

4 Answers 4

5

Use an IN query, which will grab all of the results in a single query:

SELECT * FROM people WHERE town IN('LA', 'London', 'Paris')
Sign up to request clarification or add additional context in comments.

2 Comments

And how does one then output the results in the desired fashion? This answer completely fails to address the question the OP asked.
@eggyal the real question is how to improve the efficiency of the repeated queries, which I answered. OP never actually asked how to output it in that way. If OP has specific problems with the output then I would gladly help.
4

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)

Comments

1

As @MrCode mentions, you can use MySQL's IN() operator to fetch records for all of the desired cities in one go, but if you then sort your results primarily by city you can loop over the resultset keeping track of the last city seen and outputting the new city when it is first encountered.

Using PDO, together with MySQL's FIELD() function to ensure that the resultset is in the same order as your original array (if you don't care about that, you could simply do ORDER BY city, which would be a lot more efficient, especially with a suitable index on the city column):

$arr = ['Los Angeles', 'New York', 'Chicago'];
$placeholders = rtrim(str_repeat('?, ', count($arr)), ', ');

$dbh = new PDO("mysql:dbname=$dbname", $username, $password);
$qry = $dbh->prepare("
  SELECT   city, name
  FROM     my_table
  WHERE    city IN ($placeholders)
  ORDER BY FIELD(city, $placeholders)
");

if ($qry->execute(array_merge($arr, $arr))) {

  // output headers
  echo '<ul>';

  $row = $qry->fetch();
  while ($row) {
    $current_city = $row['city'];

    // output $current_city initialisation
    echo '<li>'.htmlentities($current_city).'</li><ul>';
    do {
      // output name $row
      echo '<li>'.htmlentities($row['name']).'</li>';
    } while ($row = $qry->fetch() and $row['city'] == $current_city);
    // output $current_city termination
    echo '</ul>';
  }

  // output footers
  echo '</ul>';
}

Comments

0

That's the purpose of prepared statements. You bind a placeholder to a value, and use it like a variable, with the same query. Since the query hasn't changed, you minimize the communication with the mysql server, resulting in an efficiency boost.

Example using PDO:

$cities = array(
    "Los Angeles",
    "New York",
    "Chicago"
);

try {
    //Change database parameters here (user, pass, database name)
    $db = new PDO("mysql:host=localhost;dbname=users", "user", "pass");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    $stmt = $db->prepare("SELECT * FROM `users` WHERE `city` = :city");

    foreach ($cities as $city) {
        $stmt->bindValue(":city", $city);
        $stmt->execute();

        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        //Output data here. You can format it however you'd like
        var_dump($city, $result);
    }

}
catch (PDOException $e) {
    //Error handling here
}

1 Comment

So are you saying the method I made in my post is okay?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.