title: MySQL Database Structure - Storing Facebook FQL Results
body: <p>I'm using FQL to query the <a href="http://developers.facebook.com/docs/reference/fql/insights/" rel="nofollow">Facebook Insights table</a> and return the data to my server, where I plan to store it within a MySQL database.</p>
  
  <p>I've found that all the Insights metrics return different data types for the value field. For example, something like <strong>application_installation_adds</strong> will return a value like this:</p>
  
  <pre><code>Array
  (
      [0] =&gt; stdClass Object
          (
              [metric] =&gt; application_installation_adds
              [value] =&gt; 3
          )
  
  )
  </code></pre>
  
  <p>..while a metric like <strong>application_permission_views_top</strong> will return this if it has data:</p>
  
  <pre><code>Array
  (
      [0] =&gt; stdClass Object
          (
              [metric] =&gt; application_permission_views_top
              [value] =&gt; stdClass Object
                  (
                      [permissions_impression_email] =&gt; 5
                      [permissions_impression_user_birthday] =&gt; 4
                      [permissions_impression_read_insights] =&gt; 4
                  )
  
          )
  
  )
  </code></pre>
  
  <p>..and this if it's empty:</p>
  
  <pre><code>Array
  (
      [0] =&gt; stdClass Object
          (
              [metric] =&gt; application_permission_views_top
              [value] =&gt; Array
                  (
                  )
  
          )
  
  )
  </code></pre>
  
  <p>Given the different data types and values, I was wondering what the best way to store this data within my database would be.</p>
  
  <p>I was thinking of setting up a table like this:</p>
  
  <ul>
  <li>metric_name</li>
  <li>metric_subname</li>
  <li>value</li>
  </ul>
  
  <p>and then using a process like this:</p>
  
  <ol>
  <li>Get FQL result.</li>
  <li>If it's an empty Array, do nothing <em>(because there is no data)</em>.</li>
  <li>If it's a single value, insert metric_name and value. Give the metric_subname a value of "Singlular" <em>(so that I know it's just a one-value metric)</em>.</li>
  <li>If it's a stdCLass Object, use a foreach loop to fill in the metric_subname column.</li>
  </ol>
  
  <p>This should give me a table like so, which will allow me to query the data in an easy manner down the track: <a href="http://i.stack.imgur.com/0fekJ.png" rel="nofollow">http://i.stack.imgur.com/0fekJ.png</a></p>
  
  <p>Can anyone please provide feedback? Is this a good way to go about it, or are there better options?</p>
  
  <p>Thanks in advance! :)</p>