-1

Is it possible to transform an array in PHP into an NSArray?

PHP code:

<?php
$DB_HostName = "XXXXX";
$DB_Name = "XXXXX";
$DB_User = "XXXXX";
$DB_Pass = "XXXXX";
$DB_Table = "Name";

$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error()); 
mysql_select_db($DB_Name,$con) or die(mysql_error()); 

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$result = mysql_query("SELECT * FROM Name");

while($row = mysql_fetch_array($result))
 {

$Name = $row['name'];
$LastName = $row['lastname'];

echo "$Name $LastName";
echo "<br />";

}
mysql_close($con);

?>

Can I make "$Name" and "$LastName" to Foundation-styled NSArrays without using JSON?

2

1 Answer 1

3

First of all xcode is an IDE not a language ;) Your language of choice is objective-c.

What you have to do is do a request to the php page (if you are going to use this on a real app and not for an exercise, you'll have to put the page somewhere publicly) and get the data for processing.

Since you don't want this to be json, you can have easy options and hard options on how to get the data into an array.

One way is using delimiters. For example your output could be:

Michael Westen##Fiona Glenanne##Sam Axe

You can get that easily using the following:

NSString *dataString = [NSString stringWithContentsOfURL:url encoding:nil error:nil];
myArray = [dataString componentsSeparatedByString:@"##"];

Where url is the url of the page you want to get the data and myArray your NSArray.

1
  • Also remember that you have to output them in UTF-8, or else funky characters will come out.
    – Panagiotis
    Jul 20, 2012 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.