Guest User

Untitled

a guest
Jul 28th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. How to convert mysql data base table data in json using php
  2. <?php
  3. $host = "emriphone.db.6420177.hostedresource.com";
  4. $user = "emriphone";
  5. $pass = "Light12-";
  6. $database = "emriphone";
  7.  
  8. $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
  9. mysql_select_db($database, $linkID) or die("Could not find database.");
  10.  
  11. $sth = mysql_query("SELECT * FROM ProviderAppointmentListings");
  12. $rows = array();
  13. while($r = mysql_fetch_assoc($sth)) {
  14. $rows[] = $r;
  15. }
  16. print json_encode($rows);
  17. ?>
  18.  
  19. $query = mysql_query("SELECT * FROM table");
  20. $rows = array();
  21. while($row = mysql_fetch_assoc($query)) {
  22. $rows[] = $row;
  23. }
  24. print json_encode($rows);
  25.  
  26. if (!function_exists('json_encode'))
  27. {
  28. function json_encode($a=false)
  29. {
  30. if (is_null($a)) return 'null';
  31. if ($a === false) return 'false';
  32. if ($a === true) return 'true';
  33. if (is_scalar($a))
  34. {
  35. if (is_float($a))
  36. {
  37. // Always use "." for floats.
  38. return floatval(str_replace(",", ".", strval($a)));
  39. }
  40.  
  41. if (is_string($a))
  42. {
  43. static $jsonReplaces = array(array("\", "/", "n", "t", "r", "b", "f", '"'), array('\\', '\/', '\n', '\t', '\r', '\b', '\f', '"'));
  44. return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
  45. }
  46. else
  47. return $a;
  48. }
  49. $isList = true;
  50. for ($i = 0, reset($a); $i < count($a); $i++, next($a))
  51. {
  52. if (key($a) !== $i)
  53. {
  54. $isList = false;
  55. break;
  56. }
  57. }
  58. $result = array();
  59. if ($isList)
  60. {
  61. foreach ($a as $v) $result[] = json_encode($v);
  62. return '[' . join(',', $result) . ']';
  63. }
  64. else
  65. {
  66. foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
  67. return '{' . join(',', $result) . '}';
  68. }
  69. }
  70. }
  71.  
  72. $sql="Select * from table";
  73. $l= array();
  74. $result = mysql_query($sql);
  75. while ($row = mysql_fetch_assoc($result)) {
  76. $l[] = $row;
  77. }
  78. $j = json_encode($l);
  79. echo $j;
Add Comment
Please, Sign In to add comment