title: SQL Database storage in web browser Javascript/HTML5
body: <p>I have a problem displaying the results after executing a query in a SQLite database which is created on the run.</p>
  
  <p>Here is my code</p>
  
  <p>CREATING DATABASE</p>
  
  <pre><code>var mydb=false;
  
  // initialise the database
  initDB = function() {
    try { 
      if (!window.openDatabase) { 
        alert('not supported'); 
      } else { 
        var shortName = 'APP_DB'; 
        var version = '0.1'; 
        var displayName = 'It Happened Today DB'; 
        var maxSize = 262144; // in bytes, 256kb 
        mydb = openDatabase(shortName, version, displayName, maxSize); 
       }
    } catch(e) { 
      // Error handling code goes here. 
      if (e == INVALID_STATE_ERR) { 
        // Version number mismatch. 
        alert("Invalid database version."); 
      } else { 
        alert("Unknown error "+e+"."); 
      } 
      return; 
    } 
  }
  
   // db error handler - prevents the rest of the transaction going ahead on failure
  errorHandler = function (transaction, error) { 
    // returns true to rollback the transaction
    return true;  
  } 
  
  // null db data handler
  nullDataHandler = function (transaction, results) { } 
  </code></pre>
  
  <p>CREATING TABLE AND INSERTING VALUES</p>
  
  <pre><code> // create tables for the database
  createTables = function() {
  
    try {
     mydb.transaction(
        function(transaction) {
          transaction.executeSql('CREATE TABLE milestones (ID INT( 10 ) NOT NULL, Title    VARCHAR( 50 )    DEFAULT NULL, mYear    INT( 11 )        NOT NULL, mMonth   INT( 11 )        DEFAULT NULL,  mDay     VARCHAR( 10 )    DEFAULT NULL, mText    VARCHAR( 2000 )  NOT NULL,  Theme1   VARCHAR( 50 )    DEFAULT NULL, Theme2   VARCHAR( 50 )    DEFAULT NULL, ImageURL VARCHAR( 50 )    DEFAULT NULL, PRIMARY KEY ( ID ));', [], nullDataHandler, errorHandler); 
          transaction.executeSql('INSERT INTO [milestones] ([ID], [Title], [mYear], [mMonth], [mDay], [mText], [Theme1], [Theme2], [ImageURL]) VALUES (2, "Cotton Mather", 1721, 6, 26, "Following the recommendation of Rev. Cotton Mather, Dr. Zabdiel Boylston of Boston completes the first inoculation against smallpox in the U.S., injecting his own son and two of his slaves.", "HIAm", null, "6-26 Cotton Mather3g04597v.jpg");', [], nullDataHandler, errorHandler); 
          transaction.executeSql('INSERT INTO [milestones] ([ID], [Title], [mYear], [mMonth], [mDay], [mText], [Theme1], [Theme2], [ImageURL]) VALUES (6, "New York Hospital", 1771, 6, 13, "New York Hospital, the second in the colonies after the Pennsylvania Hospital, receives a royal charter from King George III under the name Society of the Hospital in the City of New York in America, later changed to Society of New York Hospital.", "HIAm", null, "Default.png");', [], nullDataHandler, errorHandler); 
          });
  
    } catch(e) {
      /// alert(e.message);
      return;
    }
  };
  </code></pre>
  
  <p>TEXT2HTML</p>
  
  <pre><code>milestonesDataHandler = function(transaction, results){
      var html = "";
      for(var i=0; i &lt; results.rows.length; i++){
          var row = results.rows.item(i);
          html += '&lt;li class="elist"&gt; \
          &lt;a href="article.template.html?id="&gt;'+row['id']+'data-transition="none"&gt; \
              &lt;img src="img/"&gt;'+row['ImageURL']+'" height="70" width="70" /&gt; \
              &lt;h4&gt;'+row['Title']+'&lt;/h4&gt; \
              &lt;p&gt;'+ dateFormat($row['mYear']+' '+$row['mMonth']+' '+$row['mDay'])+'&lt;/p&gt; \
          &lt;/a&gt; \
          &lt;/li&gt;';
      }
  }
  </code></pre>
  
  <p>EXECUTE QUERY</p>
  
  <pre><code>loadMilestones = function(){
  try {
      mydb.transaction(
          function(transaction){
              transaction.executeSql("SELECT * FROM milestones", [], milestonesDataHandler, errorHandler);
           });
  }
   catch(e) {
      alert(e.message);
    }
  </code></pre>
  
  <p>}</p>
  
  <p>AT THE END OF THE HTML DOCUMENT I DO: </p>
  
  <pre><code>&lt;script&gt;$("div.miles").html(milestonesDataHandler);&lt;/script&gt;
  </code></pre>
  
  <p>Nothing gets display. When I go to the Chrome debugger it says: 131 Uncaught TypeError: Cannot read property 'length' for undefined. </p>
  
  <p>This is referring to the 3rd line in the TEXT2HTML part. It looks like for some reason, the results variable is not being filled.</p>
  
  <p>Would you please help me solve this problem.</p>
  
  <p>Thanks</p>