title: Objective-C and MySQL
body: <p>I've been able to connect to a MySQL database in my app and use the C API which is almost exactly like PHP commands (mysql_real_connect(), mysql_query(), mysql_fetch_array(), etc.) and which I am pretty comfortable with I'm just not sure how the data query is returned. Do I use an array or dictionary and then how would I parse it. For example, in PHP I would do something like so (after the connection):</p>
  
  <pre><code>$results = mysql_query("SELECT * FROM theDatabase"); 
  if (mysql_num_rows($results) &gt; 0) { 
      while($row = mysql_fetch_array($results)) {
         print $row;
      }
  }
  </code></pre>
  
  <p>What would the objective-c equivalent be? Thanks.</p>
  
  <p>Edit: </p>
  
  <p>OK, so I made some progress  - I can make the query and get the number of fields/rows returned, just can't seem to access the data itself. Here's my code, which I stitched together from the MySQL docs and a few other sites:</p>
  
  <pre><code>- (IBAction)dbConnect:(id)sender {
  
  
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  MYSQL mysql;
  mysql_init(&amp;mysql);
  
  if (!mysql_real_connect(&amp;mysql, "10.1.1.99", "******", "******", "oldphotoarchive", 0, NULL, 0)) {
      NSLog(@"%@", [NSString stringWithUTF8String:mysql_error(&amp;mysql)]);
  } else { 
  
      MYSQL_RES *result;
      MYSQL_ROW row;
      unsigned int num_fields;
      unsigned int num_rows;
      unsigned long *lengths;
  
      if (mysql_query(&amp;mysql,"SELECT * FROM photorecord")) {
          // error
      } else { // query succeeded, process any data returned by it  
  
          result = mysql_store_result(&amp;mysql);
          if (result)  {
              num_fields = mysql_num_fields(result);
              while ((row = mysql_fetch_row(result))) {
                  lengths = mysql_fetch_lengths(result);
  
                  for(int i = 0; i &lt; num_fields; i++) {
                                              //the line below is my problem, printing row[i] fails, I get the GNU gdb error...
                      row[i] ? NSLog(@"%@", row[i]) : NSLog(@"wtf");
                  }
              }
  
  
          } else  {// mysql_store_result() returned nothing; should it have?
              if (mysql_errno(&amp;mysql)) {
                  NSLog(@ "Error: %s\n", mysql_error(&amp;mysql));
              } else if (mysql_field_count(&amp;mysql) == 0) {
                  // query does not return data
                  // (it was not a SELECT)
                  num_rows = mysql_affected_rows(&amp;mysql);
              }
          }
  
      }
  
  }
  
  [pool release];
  }
  </code></pre>