title: PHP script to select MySQL data fields and return JSON
body: <p>So I am trying to download JSON into an iOS application and I don't really know a lot about PHP. </p>
  
  <p>Currently I'm using a generic php scrip as a delegate that connects the the MySQL database and returns JSON results. </p>
  
  <pre><code>&lt;?php
  $host = "localhost"; //database host server
  $db = "***"; //database name
  $user = "root"; //database user
  $pass = "root"; //password
  
  $connection = mysql_connect($host, $user, $pass);
  
  //Check to see if we can connect to the server
  if(!$connection)
  {
      die("Database server connection failed.");  
  }
  else
  {
      //Attempt to select the database
      $dbconnect = mysql_select_db("***", $connection);
  
      //Check to see if we could select the database
      if(!$dbconnect)
      {
          die("Unable to connect to the specified database!");
      }
      else
      {
          $query = "SELECT * FROM table";
          $resultset = mysql_query($query, $connection);
  
          $records = array();
  
          //Loop through all our records and add them to our array
          while($r = mysql_fetch_assoc($resultset))
          {
              $records[] = $r;        
          }
  
          //Output the data as JSON
  
          echo json_encode($records);
  
      }
  
  
  }
  
  
  ?&gt;
  </code></pre>
  
  <p>The issue with this is that I don't want to encode the entire table in JSON I just want to return a few select data fields from the particular table so I don't download unnecessary data. </p>
  
  <p>I know that you do this in the SELECT query but something is wrong with my syntax for this. </p>
  
  <p>I had been trying with:</p>
  
  <pre><code>$query = "SELECT *[datafield],[otherdatafield],... FROM table";
  </code></pre>
  
  <p>but that doesnt seem to be working.</p>
  
  <p>Also, In that table there are two separate data fields that are used to build a URL for an image, and Im not sure how to combine the two here so that they are returned in the JSON as one field. </p>
  
  <p>For example: I Have a base string </p>
  
  <pre><code>"http://wwww.mysite.com/" 
  </code></pre>
  
  <p>and i would like the add the two data Fields to that string so that when it returns the JSON objects they have those fields already concatenated so that the app can just use that URL: </p>
  
  <pre><code>"http://wwww.mysite.com/[data field1]/[datafield2]" 
  </code></pre>