Hello today we will create the JSON from php Array. As now these days we are using jquery and ajax too much and we need it very often.
You can read the whole json encoding at php official website. As json_encode(Array) expects parameter as Array. So here we will use the php array. Here i am creating the database and we use to take out the value from Id and use it to show it in HTML Form.
Step 1. Create Table in Database
We will create a table Member with 3 columns you can create your own and use your own naming.
We completed the set up of the table and also please insert some values in it.
Step 2. Create Member Table values Array
Now we will create the array of member table values by using the mysqli_fetch_assoc($result). In this example we are taking the values by ID.
query = select * from member where Id = 2;
Now i am getting the values from member table where Id = 2. Now we will save it in an array by using the mysqli_fetch_assoc(). and than we will create the JSON of that array.
Step 3. Return JSON
We will get our values by using the below code into the JSON.
$row= mysqli_fetch_assoc($this->result); return json_encode($row);
Now it will return the json.
Step 4. Use JSON
Now after getting the JSON we will use the values in from or … where ever you want to use it. 🙂
$('#fname').val(obj.Full_name); $('#phone').val(obj.Mobile); $('#id').val(obj.Id);
Now you will see the database values in your from fields.
Tell us how you find our article about creating JSON from PHP Array using Mysql. If you have any issues than please comment below. 🙂