sepoto Posted April 8, 2012 Share Posted April 8, 2012 <?php$link = mysql_connect('localhost', 'root', '');if (!$link) { die('Connection error: ' . mysql_error());} $result = mysql_query('use `mydatabase`');if(!result) {die('Query error:' . mysql_error());} $result = mysql_query('select * from locations'); mysql_close($link); echo json_encode($result);?> I have a sample I prepared that I am working on to get it functional. I see that in the reference for json_encode that the parameter is not allowed to be of the type resource. This leads to the problem of what to do with $result. I suppose I could use a for loop and populate an array and then pass that array to json_encode. I guess the real reason why I post this is to see if someone has some trick to do this that I may not be aware of. Thoughts? Link to comment Share on other sites More sharing options...
sepoto Posted April 8, 2012 Author Share Posted April 8, 2012 while($row = mysql_fetch_assoc($table)){ foreach($row as $key => $value) { $arr[$key] = $value; } $main_arr[] = $arr;}$json = json_encode($main_arr); I found this above which is a great example! Is there another way? Link to comment Share on other sites More sharing options...
boen_robot Posted April 8, 2012 Share Posted April 8, 2012 I believe that if you use MySQLi in PHP 5.4, the result is a serializable object (=> should be compatible with json_encode). Link to comment Share on other sites More sharing options...
birbal Posted April 9, 2012 Share Posted April 9, 2012 also mysqli has a function which you can use directly to encode http://in3.php.net/manual/en/mysqli-result.fetch-all.php Pleas dont use mysql extension it is obsolete now. use mysqli instead or more better http://php.net/pdo Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.