0

I have a for loop to cycle through and array, run a database query in relation to each element, then call a function that prints out something in relation to it. The array is 12 elements long but the for loop never gets past element 0. It doesn't error or fail it just doesn't do anything after the first element. I verified that by putting the echo $x; and echo $vendorsname[$x]; at the start of each loop cycle and sure enough it only ever echo's 0 out to the page.

$continuetill = count($vendorsname);

for ($x = 0; $x < $continuetill; $x++)
{

echo $x;
echo $vendorsname[$x];

$sql="SELECT low,mid,high,verlow,vermin,verhigh FROM vendors WHERE vendor = ".$x." ORDER BY id DESC LIMIT 1";

if ($result=mysqli_query($conn,$sql))
  {
  // Fetch one and one row
  while ($row=mysqli_fetch_row($result))
    {
      $low = $row[0];
      $mid = $row[1];
      $high = $row[2];
      $verlow = $row[3];
      $vermid = $row[4];
      $verhigh = $row[5];



      if(($low > $mid) && ($low > $high))
      {
        likely295Message($vendorsname[$x]);
      }
      elseif (($high > $low) && ($high > $mid) && ($high < 15))
      {
        possibly300Message($vendorsname[$x]);
      }
      elseif (($high > $low) && ($high > $mid) && ($high >= 15))
      {
        likely300Message($vendorsname[$x]);
      }
      elseif (($mid > $low) && ($mid > $high))
      {
        likely296Message($vendorsname[$x]);

      }else
      {
        unknownMessage($vendorsname[$x]);
      }


      if(($verlow != 0) || ($vermid != 0) || ($verhigh != 0))
      {
        if(($verlow > $vermid) && ($verlow > $verhigh))
        {
          verified295Message($vendorsname[$x]);
          changeBackgroundBack($vendorsname[$x]);
          changeImage($vendorsname[$x]);

        }
        elseif (($verhigh > $verlow) && ($verhigh > $vermid))
        {
          verified300($vendorsname[$x]);
          changeBackground($vendorsname[$x]);
          changeImage($vendorsname[$x]);
        }
        elseif (($vermid > $verlow) && ($vermid > $verhigh))
        {
          verified296($vendorsname[$x]);
          changeBackgroundBack($vendorsname[$x]);
          changeImage($vendorsname[$x]);
        }
      }

    }

  mysqli_free_result($result);
}
}
Flatlyn
  • 2,040
  • 6
  • 40
  • 69
  • It's wrong to iterate fetching from database inside loops. In your case it's better to do something like this: `$sql="SELECT low,mid,high,verlow,vermin,verhigh FROM vendors WHERE vendor IN (".implode(',', range(0, count($)).") ORDER BY id DESC";` – MAZux Oct 07 '17 at 19:55
  • Make sure about the value of `$continuetill`, if it is `0` then it will print `0` for `$x`. You can use `foreach()` loop for your array. – Mofiqul Islam Oct 07 '17 at 20:19
  • @MofiqulIslm Yeah I had verified it's value. It was a mis-spelled function and I had missed turning error reporting on at the top of the script. – Flatlyn Oct 07 '17 at 20:19
  • 1
    **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or **any** user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Oct 08 '17 at 00:20
  • A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so mistakes aren't easily ignored. – tadman Oct 08 '17 at 00:20
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…”)` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Oct 08 '17 at 00:20
  • @tadman Yeah I know that for user data. This is pure server side though so no user data is being collected anywhere on the page with POST, GET, or anything else. – Flatlyn Oct 08 '17 at 00:22
  • 1
    It's still a super bad habit to get into, and you should make every possible effort to avoid it. Sometimes code like that finds itself being pasted into other places where the data is not safe, and then you're exposed to serious problems. – tadman Oct 08 '17 at 00:24
  • 1
    @tadman You’re right. I’ll update my code. – Flatlyn Oct 08 '17 at 00:27

1 Answers1

0

Make sure you have error displaying turned on. Add at the beginning of your script:

ini_set('display_errors', 1);

to make sure you don't have any errors.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • The sql statement and the mysqli_query run inside the loop. It should create that statement and run the query for each element in the array so it should run it every time the loop does. – Flatlyn Oct 07 '17 at 19:48
  • Yeah triple checked that. But even if they didn’t exist it should still print that echo $x; for each cycle of the for loop and it’s only print 0 so only running once. – Flatlyn Oct 07 '17 at 19:53
  • Are you sure you have error displaying turned on? `ini_set('display_errors', 1);` ? – Marcin Nabiałek Oct 07 '17 at 19:54
  • That was it. I had missed error displaying and one of the function calls was mis-spelled. If you want to edit your answer to mention turning on error displaying I'll mark it correct. – Flatlyn Oct 07 '17 at 20:12