title: Array loosing values
body: <p>It seems simple enough, query 15 records and put them in an array.
  The code compiles and runs, but the array looses the values entered into it.
  I print the values as they are entered into the array, and it appears correct.  Though in a for loop, the first row is correct but incorrect after that.
  The code:</p>
  
  <pre><code>printf("SQL returned %d rows\n", mysql_num_rows(res)) ;
  
  i = 0 ;
  
  while ((row = mysql_fetch_row(res)) != NULL) 
  {
      sscanf(row[0], "%lf", &amp;data[i][0]) ;
      sscanf(row[1], "%lf", &amp;data[i][1]) ;    // bid will always be below ask
      sscanf(row[2], "%lf", &amp;data[i][2]) ;    // offer = ask in this DB
      sscanf(row[3], "%lf", &amp;data[i][3]) ;    // mid = average of all bid/ask for each minute
      data[i][4] = 0 ;                        // MA5
      data[i][5] = 0 ;                        // MA15
  
      printf("%.0lf   %lf %lf %lf %lf %lf\n", data[i][0], data[i][1], data[i][2], data[i][3], data[i][4], data[i][5]) ;
  
      i++ ;
  }
  
  printf("\n") ;
  
  for (j=0; j&lt;15; j++)
      printf("%.0lf   %lf %lf %lf %lf %lf\n", data[j][0], data[j][1], data[j][2], data[j][3], data[j][4], data[j][5]) ;
  </code></pre>
  
  <p>The first 15 rows match what is in the database, but only the first row of the for loop is printing correct values.</p>
  
  <pre><code>SQL returned 15 rows
  1506594851  150.912000  150.932000  150.909265  0.000000    0.000000
  1506594792  150.921000  150.941000  150.925761  0.000000    0.000000
  1506594731  150.940000  150.963000  150.959714  0.000000    0.000000
  1506594674  150.949000  150.971000  150.969609  0.000000    0.000000
  1506594611  150.954000  150.976000  150.957382  0.000000    0.000000
  1506594551  150.940000  150.964000  150.935012  0.000000    0.000000
  1506594492  150.906000  150.927000  150.910357  0.000000    0.000000
  1506594431  150.881000  150.906000  150.865748  0.000000    0.000000
  1506594371  150.822000  150.846000  150.821062  0.000000    0.000000
  1506594851  150.912000  150.932000  150.909265  0.000000    0.000000
  1506594251  150.768000  150.794000  150.751332  0.000000    0.000000
  1506594194  150.701000  150.724000  150.715379  0.000000    0.000000
  1506594134  150.740000  150.757000  150.754935  0.000000    0.000000
  1506594076  150.749000  150.772000  150.758302  0.000000    0.000000
  1506594011  150.755000  150.774000  150.753550  0.000000    0.000000
  
  1506594851  150.912000  150.932000  150.909265  0.000000    0.000000
  1506594251  150.768000  150.794000  150.751332  0.000000    0.000000
  1506594194  150.701000  150.724000  150.715379  0.000000    0.000000
  1506594134  150.740000  150.757000  150.754935  0.000000    0.000000
  1506594076  150.749000  150.772000  150.758302  0.000000    0.000000
  1506594011  150.755000  150.774000  150.753550  0.000000    0.000000
  1506594492  150.906000  150.927000  150.910357  0.000000    0.000000
  1506594431  150.881000  150.906000  150.865748  0.000000    0.000000
  1506594371  150.822000  150.846000  150.821062  0.000000    0.000000
  1506594312  150.798000  150.824000  150.798255  0.000000    0.000000
  0   0.000000    97795656476117789550858635641226156856160388037177544822378925818587375507523129063032745851654482135492440234643161088.000000  270721760232452916408621777331997134952922302290494666767651053623661202019325513025428600003963936853860599692342289934319616.000000   0.000000    0.000000
  0   0.000000    0.000000    0.000000    0.000000    0.000000
  0   0.000000    0.000000    0.000000    0.000000    0.000000
  0   0.000000    0.000000    0.000000    0.000000    0.000000
  0   0.000000    0.000000    0.000000    0.000000    0.000000
  </code></pre>
  
  <p>I am unable to figure out why the array is changing, though the print statements are identical, only one in a <em>while</em> and the other in a <em>for</em> loop.
  Thanks in advance</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "/usr/include/mysql/mysql.h"
  #include &lt;string.h&gt; // strncpy
  
  
  int main(void)
  {
  
  int i = 0 ;
  int j = 0 ;
  int n = 0 ;
  int position = 0 ;
  double pairsid = 8 ;
  char sql[300] ;
  
  double tmpdbl = 0 ;
  
  double data[6][15] ;    // Stamp, bid, ask, mid, SMA5, SMA15
  
  char *server = "localhost" ;
  char *user = "x" ;
  char *password = "x" ; // set me first
  char *database = "x";
  
  MYSQL *con1 ;
  MYSQL_RES *res ;
  MYSQL_ROW row ;
  con1 = mysql_init(NULL) ;
  
  // Connect to database
  if (!mysql_real_connect(con1, server, user, password, database, 0, NULL, 0)) 
  {
      fprintf(stderr, "DB Connect Error %s\n", mysql_error(con1)) ;
      exit(1);
  }
  
  
  sprintf(sql, "(SELECT stamp, bid, ask, mid FROM oanda WHERE pairsid = %.0lf ORDER BY stamp LIMIT %d) ORDER BY stamp DESC", pairsid, 15) ;
  
  printf("%s\n", sql) ;
  
  if (mysql_query(con1, sql)) 
  {
      fprintf(stderr, "Error running query %s\n", mysql_error(con1)) ;
      exit(1) ; 
  }
  
  res = mysql_store_result(con1) ;    
  
  
  printf("SQL returned %d rows\n", mysql_num_rows(res)) ;
  
  i = 0 ;
  
  while ((row = mysql_fetch_row(res)) != NULL) 
  {
      sscanf(row[0], "%lf", &amp;data[i][0]) ;
      sscanf(row[1], "%lf", &amp;data[i][1]) ;    // bid will always be below ask
      sscanf(row[2], "%lf", &amp;data[i][2]) ;    // offer = ask in this DB
      sscanf(row[3], "%lf", &amp;data[i][3]) ;    // mid = average of all bid/ask for each minute
      data[i][4] = 0 ;                        // MA5
      data[i][5] = 0 ;                        // MA15
  
      printf("%.0lf   %lf %lf %lf %lf %lf\n", data[i][0], data[i][1], data[i][2], data[i][3], data[i][4], data[i][5]) ;
  
      i++ ;
  }
  
  printf("\n") ;
  
  for (j=0; j&lt;15; j++)
      printf("%.0lf   %lf %lf %lf %lf %lf\n", data[j][0], data[j][1], data[j][2], data[j][3], data[j][4], data[j][5]) ;
  
  printf("\n") ;
  
  mysql_free_result(res) ;
  
  mysql_close(con1) ;
  }
  </code></pre>
  
  <p>OK, got it working now.  Thanks all</p>
  
  <pre><code>while ((row = mysql_fetch_row(res)) != NULL) 
  {
      sscanf(row[0], "%lf", &amp;data[0][i]) ;
      sscanf(row[1], "%lf", &amp;data[1][i]) ;    // bid will always be below ask
      sscanf(row[2], "%lf", &amp;data[2][i]) ;    // offer = ask in this DB
      sscanf(row[3], "%lf", &amp;data[3][i]) ;    // mid = average of all bid/ask for each minute
      data[4][i] = 0 ;                        // MA5
      data[5][i] = 0 ;                        // MA15
  
      printf("Row %d %.0lf    %lf %lf %lf %lf %lf\n", i, data[0][i], data[1][i], data[2][i], data[3][i], data[4][i], data[5][i]) ;
  
      i++ ;
  }
  
  printf("\n") ;
  
  for (j=0; j&lt;15; j++)
      printf("%.0lf   %lf %lf %lf %lf %lf\n", data[0][j], data[1][j], data[2][j], data[3][j], data[4][j], data[5][j]) ;
  </code></pre>
  
  <p>gives me </p>
  
  <pre><code>SQL returned 15 rows
  Row 0 1506594851    150.912000  150.932000  150.909265  0.000000    0.000000
  Row 1 1506594792    150.921000  150.941000  150.925761  0.000000    0.000000
  Row 2 1506594731    150.940000  150.963000  150.959714  0.000000    0.000000
  Row 3 1506594674    150.949000  150.971000  150.969609  0.000000    0.000000
  Row 4 1506594611    150.954000  150.976000  150.957382  0.000000    0.000000
  Row 5 1506594551    150.940000  150.964000  150.935012  0.000000    0.000000
  Row 6 1506594492    150.906000  150.927000  150.910357  0.000000    0.000000
  Row 7 1506594431    150.881000  150.906000  150.865748  0.000000    0.000000
  Row 8 1506594371    150.822000  150.846000  150.821062  0.000000    0.000000
  Row 9 1506594312    150.798000  150.824000  150.798255  0.000000    0.000000
  Row 10 1506594251   150.768000  150.794000  150.751332  0.000000    0.000000
  Row 11 1506594194   150.701000  150.724000  150.715379  0.000000    0.000000
  Row 12 1506594134   150.740000  150.757000  150.754935  0.000000    0.000000
  Row 13 1506594076   150.749000  150.772000  150.758302  0.000000    0.000000
  Row 14 1506594011   150.755000  150.774000  150.753550  0.000000    0.000000
  
  1506594851  150.912000  150.932000  150.909265  0.000000    0.000000
  1506594792  150.921000  150.941000  150.925761  0.000000    0.000000
  1506594731  150.940000  150.963000  150.959714  0.000000    0.000000
  1506594674  150.949000  150.971000  150.969609  0.000000    0.000000
  1506594611  150.954000  150.976000  150.957382  0.000000    0.000000
  1506594551  150.940000  150.964000  150.935012  0.000000    0.000000
  1506594492  150.906000  150.927000  150.910357  0.000000    0.000000
  1506594431  150.881000  150.906000  150.865748  0.000000    0.000000
  1506594371  150.822000  150.846000  150.821062  0.000000    0.000000
  1506594312  150.798000  150.824000  150.798255  0.000000    0.000000
  1506594251  150.768000  150.794000  150.751332  0.000000    0.000000
  1506594194  150.701000  150.724000  150.715379  0.000000    0.000000
  1506594134  150.740000  150.757000  150.754935  0.000000    0.000000
  1506594076  150.749000  150.772000  150.758302  0.000000    0.000000
  1506594011  150.755000  150.774000  150.753550  0.000000    0.000000
  </code></pre>