title: why the while loop runs once?
body: <p>the following code is only running once , while the number of times it should run is 4 , any help ?</p>
  
  <p><strong>PHP::</strong></p>
  
  <pre><code>&lt;?php
  
  header("Content-Type: application/json");
  
  require_once("config.php");
  
  if(isset($_GET["m"])) {
  
      $dirname = "images/main/";
      $arr = array();
  
      $conn = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);
  
      if(!$conn) {
          echo "Error connecting to database";
          exit();
      }
      if($stmt = $conn-&gt;prepare("SELECT name_ FROM projects")) {
          $stmt-&gt;execute();
          $stmt-&gt;bind_result($n);
          //$stmt-&gt;store_result();
          $result = $stmt-&gt;get_result();
          if($result-&gt;num_rows == 0) {
              echo "No Projects";
              $stmt-&gt;close();
              $conn-&gt;close();
              exit();
          }else {
              while ($row = $result-&gt;fetch_assoc()) {
                  $dirname = $dirname . $row["name_"] . "/";
                  $images = glob($dirname . "*.*", GLOB_BRACE);
                  foreach($images as $image) {
                      echo $row["name_"];
                      echo$result-&gt;num_rows;  // returns 4 !!!!
                      $image = base64_encode($image);
                      //$arr[] = $image;
                      array_push($arr, $image);
                      $image = "";
                  }
              }
              echo json_encode($arr);  // returns 1 json row oonly
          }
      }
  
      $stmt-&gt;close();
      $conn-&gt;close();
      exit();
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>num rows return 4 so why it runs or loops for one time only ?</p>
  
  <p>I am trying to get images from images folder to echo it back</p>
  
  <p><strong>FIX::</strong></p>
  
  <p>according to jhilgeman's answer I added this part to the end of foreach:</p>
  
  <pre><code>$dirname = "images/main/";
  </code></pre>