I have to return data fetched from MySQL table into a php file as JSON. Here is my code to connect to mysql & get data from it. How could now I return it as JSON.
<?php
$username = "user";
$password = "********";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";
$selected = mysql_select_db("spec",$dbh)
or die("Could not select first_test");
//$rows = array();
$query = "SELECT * FROM user_spec";
$result=mysql_query($query);
//mysql_close($dbh);
?>
Under is full stack that I have to implement. For step 3, I am rendering the list dynamically using user inputs though its not using any engine but directly using input values so I have to see it how to do once I get JSON data. I put the stack so that you people can kindly see it what I have to do when possibly helping me.
- the user load an HTML page
- the page make an ajax call and get the options as a JSON(either it exists already in the database, or a new option set is generated)
- the json is rendered using a JS templating engine (PURE in our case)
- the user change something
- the same JSON is modified and sent by a POST to the server
- the server read that JSON and store it in the database(you would write the data to your file). And then go back to the step 4, to wait for another user change.