title: Undefined offset: -1 PDO in for loop
body: <p>I use pdo to connect mysql database and with PDO::FETCH_ASSOC function .. fetch ASSOC array..</p>
  
  <p>But when run the code give this error in multi lines.</p>
  
  <blockquote>
    <p>Undefined offset: -1 or Undefined offset: 61</p>
  </blockquote>
  
  <pre><code>if($pays[$i]['price'] &lt; $pays[($i-1)]['price'])
  </code></pre>
  
  <p>and</p>
  
  <pre><code>elseif($pays[$i]['price'] &gt; $pays[($i-1)]['price'])
  </code></pre>
  
  <p>and </p>
  
  <p><code>.$pays[$i]['price'].</code></p>
  
  <p>My full code</p>
  
  <pre><code>&lt;?php
  
  $params        = null; //or any params
  $mrkfPDO       = new PDO('mysql:host=localhost;dbname=usd', 'root', '', array(
      PDO::ATTR_PERSISTENT =&gt; true,
      PDO::MYSQL_ATTR_INIT_COMMAND =&gt; "SET NAMES utf8"
  ));
  $mrkfStatement = $mrkfPDO-&gt;prepare("SELECT price FROM uds limit 0,100");
  $mrkfStatement-&gt;execute($params);
  $pays = $mrkfStatement-&gt;fetchAll(PDO::FETCH_ASSOC);
  
  for ($i = 0; $i &lt;= count($pays); $i++) {
  
      $color = 'black';
  
      if ($pays[$i]['price'] &lt; $pays[($i - 1)]['price'])
          $color = "red";
      elseif ($pays[$i]['price'] &gt; $pays[($i - 1)]['price'])
          $color = 'green';
      else
          $color = 'black';
  
      echo "&lt;tr&gt;
                          &lt;td&gt;
                              &lt;span style='color: $color'&gt;" . $pays[$i]['price'] . "&lt;/span&gt;
                          &lt;/td&gt;
                        &lt;/tr&gt;";
  }
  
  ?&gt;
  </code></pre>