2

I'm rather new to ios and json.

I've managed to create a db in mysql and used php to create a basic read and write php pages.

I'm using the TwitterHelper class from cs193p class for the presence assignment from Stanford to acces the php interface online and tie it to my ios code. I am getting an error which I can't solve.

Update:

Ok, here is my code:

<?php

include_once("JSON.php");
$json = new Services_JSON();

$link = mysql_pconnect("localhost", "user", "pass") or die("Could not connect");
mysql_select_db("mydb") or die("Could not select database");

$query = "SELECT * FROM tags";
$result = mysql_query($query);

$arr = array();
$rs = mysql_query("SELECT * FROM tags");
 
while($obj = mysql_fetch_object($rs)) {
    $arr[] = $obj;
}
 
Echo $json->encode($arr);
//Echo '{:'.$json->encode($arr).'}';

?>

The problem is that unless i remove the [] from $arr[], i get the answer to the php page enclosed in [] which the json fetch doesnt like and therefore throws and exception error and crashes my app.

If i remove it, the php page only returns 1 result...

2
  • 2
    Post the error and the relevant parts of the source code here. Do NOT post a wall of code. Commented Sep 18, 2010 at 18:53
  • 2
    No, you can't e-mail us your code for help. That's not this site is for. Stack Overflow is designed so people can ask well-written questions about programming problems and get well-written answers back. Please clarify exactly what error you're getting, and where, and why. Commented Sep 18, 2010 at 20:31

2 Answers 2

1

What does Services_JSON do? In particular Service_JSON->encode(). And would a simple json_encode do the job? (since you do nothing else with the Service_JSON Object anyway)

Edit

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
header('Content-type: application/json');
echo json_ecode($arr);

output: {"a":1,"b":2,"c":3,"d":4,"e":5}

Accordig to the PEAR::Services_JSON::docs

$json = new Service_JSON;
echo $json->encode($arr);

output: ["a":1,"b":2,"c":3,"d":4,"e":5]

Sign up to request clarification or add additional context in comments.

Comments

0

the while loop is an iterator. if you want to fetch all the value from the database then the echo statement should be specified within the while loop.

Comments

Your Answer

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