Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7661153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:33:38+00:00 2026-05-31T13:33:38+00:00

How to convert MySQL data base table into JSON data using PHP. Is there

  • 0

How to convert MySQL data base table into JSON data using PHP. Is there any way to do this?

Below is the php code I am using:

<?php 
$host = "emriphone.db.6420177.hostedresource.com"; 
$user = "emriphone"; 
$pass = "Light12-"; 
$database = "emriphone"; 

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
mysql_select_db($database, $linkID) or die("Could not find database."); 

$sth = mysql_query("SELECT * FROM ProviderAppointmentListings");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
   $rows[] = $r;
}
print json_encode($rows);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:33:38+00:00Added an answer on May 31, 2026 at 1:33 pm

    Try like this:

    $query = mysql_query("SELECT * FROM table");
    $rows = array();
    while($row = mysql_fetch_assoc($query)) {
        $rows[] = $row;
    }
    print json_encode($rows);
    

    If you don’t have json_encode add this before the code above:

    if (!function_exists('json_encode'))
    {
      function json_encode($a=false)
      {
        if (is_null($a)) return 'null';
        if ($a === false) return 'false';
        if ($a === true) return 'true';
        if (is_scalar($a))
        {
          if (is_float($a))
          {
            // Always use "." for floats.
            return floatval(str_replace(",", ".", strval($a)));
          }
    
          if (is_string($a))
          {
            static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
            return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
          }
          else
            return $a;
        }
        $isList = true;
        for ($i = 0, reset($a); $i < count($a); $i++, next($a))
        {
          if (key($a) !== $i)
          {
            $isList = false;
            break;
          }
        }
        $result = array();
        if ($isList)
        {
          foreach ($a as $v) $result[] = json_encode($v);
          return '[' . join(',', $result) . ']';
        }
        else
        {
          foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
          return '{' . join(',', $result) . '}';
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using PHP, I can convert MySQL data or static table data to csv, Excel,
I am trying to convert a mySQL query into a JSON object. This works:
In PHP, I want to insert into a database using data contained in a
I have a legacy mysql database and there's this table which has a few
I pass form data to php using jquery code then the php code save
I want to convert the mysql database table contents to an Excel(.xls) or comma
I'm trying to convert my MySQL create table statements to SQLite create table statements.
What is the most efficient way to convert a MySQL query to CSV in
I have a unix timestamp that I would like to convert using mysql if
I see that within MySQL there are Cast() and Convert() functions to create integers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.