iphone - got stuck converting MySql output in JSON array with php -


i making iphone app. part of app allows users search company on location.

i have mysql database containing companies can searched for, , php file on website receive searched data, , return companyname , companylocation found companies app. looks this:

<?php  if (isset($_get["companycitysearchfield"])){                 $companycity = $_get["companycitysearchfield"];                 $result = search($companycity);                 echo $result;         }  function makesqlconnection() { $db_hostname = "******"; $db_name = "*******"; $db_user = "*******"; $db_pass = "*******";       $con = mysql_connect($db_hostname,$db_user,$db_pass) or die(mysql_error());      mysql_select_db($db_name,$con) or die(mysql_error());       return $con; }  function disconnectsqlconnection($con) {     mysql_close($con); }  function search($companycity) {     $con = makesqlconnection();      $query = mysql_query("select companyname, companycity company  companycity = '$companycity'");     $companies = array();         while ($row = mysql_fetch_assoc($query)) {         $companies['companies'][] = $row;         print json_encode($companies);     }        disconnectsqlconnection($con);   }  ?> 

this works fine when 1 company found. gives me perfect json array:

{"companies":[{"companyname":"dijkstra","companycity":"geldermalsen"}]} 

everything fine far.

now, create company in database, geldermalsen location.

2 companies found in database now. json array return now, doesn't make sense:

{"companies":[{"companyname":"dijkstra","companycity":"geldermalsen"}]}{"companies":[{"companyname":"dijkstra","companycity":"geldermalsen"},{"companyname":"testaccount","companycity":"geldermalsen"}]} 

for reason, seems make 2 separate array's. 1 first found company, , 1 both.

i have been searching web, stackoverflow, google , book 'php , mysql dummies' days, , have changed code numerous times, , whatever try keeps on doing this.

does know should one array containing found companies script, instead of these 2?

any welcome, thank in advance!

you echoing out json each row, not built array. move print statement outside loop.

$companies = array();     while ($row = mysql_fetch_assoc($query)) {     $companies['companies'][] = $row;  }  print json_encode($companies); 

or better yet, might not want echo out @ in search function, leave caller. seems might intending here:

$result = search($companycity); echo $result; 

the problem search() function doesn't return value $result null. should make mind going echo result client , consistent it.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -