title: Connect to an online database (mySQL) from Swift, using PHP. (XCODE)
body: <p>I want to connect my xcode app to online database and get the data from it and display in my app + write the data into online database using my app. I've already done with app but now it gives me an error.</p>
  
  <p>ERROR :
  <img src="https://i.stack.imgur.com/i51CN.png" alt="enter image description here"></p>
  
  <p>I have my online database in my web page and i have uploaded two php files into the file manager in my web. One php file retrieving all the data in my database and encoding them to json. And second php file doing the query to write data into my online database from my app.</p>
  
  <p><img src="https://i.stack.imgur.com/fK5Mp.png" alt="enter image description here"></p>
  
  <p>As in above pic im getting json output successfully but when i try to get the data  into an array in xcode it gives me that error. </p>
  
  <p><strong>This is my code</strong></p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  
      @IBOutlet var tableview: UITableView!
      @IBOutlet var inputFriendName: UITextField!
      @IBOutlet var inputFriendInfo: UITextField!
  
      var data: NSArray = []
  
      override func viewDidLoad() {
          super.viewDidLoad()
          data = dataOfJson("http://bishanonline.com/extra/serviceselect.php")
          println(data)
  
      }
  
      @IBAction func reload() {
          data = dataOfJson("http://bishanonline.com/extra/serviceselect.php")
          self.tableview.reloadData()
      }
  
      override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
          self.view.endEditing(true)
      }
  
      func dataOfJson(url: String) -&gt; NSArray {
          var data = NSData(contentsOfURL: NSURL(string: url)!)
          return (NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSArray)
      }
  
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return data.count
      }
  
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
          var cell: additionInfoCell = self.tableview.dequeueReusableCellWithIdentifier("customCell") as additionInfoCell
          var maindata = (data[indexPath.row] as NSDictionary)
          cell.friendName!.text = maindata["Name"] as? String
          cell.friendInfo!.text = maindata["Additional Info"] as? String
          return cell
      }
  
      @IBAction func uploadToDatabase() {
          var url: NSString = "http://bishanonline.com/extra/servicequery.php?x=\(inputFriendName.text)&amp;y=\(inputFriendInfo.text)"
          url = url.stringByReplacingOccurrencesOfString(" ", withString: "%20")
          url = url.stringByReplacingOccurrencesOfString("/n", withString: "%0A")
          var data = NSData(contentsOfURL: NSURL(string: url)!)
          var result = NSString(data: data!, encoding: NSUTF8StringEncoding)
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  }
  </code></pre>
  
  <p><strong>Issue is in this code lines</strong></p>
  
  <pre><code> func dataOfJson(url: String) -&gt; NSArray {
      var data = NSData(contentsOfURL: NSURL(string: url)!)
      return (NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSArray)
  }
  </code></pre>
  
  <p>Please help me to get json data into array. Appreciate any help.</p>