0

I have started to learn JSON with PHP. First I wan't to learn, how to create some easy API which will get some data from database from main server and use it in other webpage. I have found some solutions which will get these data into web browser console and not just specific data I need for that web but all data from database.

For example:

DATABASE

id: 1 name: test status: active

id: 2 name: test2 status: disable

INTO TEST.COM WEB

It get just record whos name is TEST and use its status.

$status = RECORD FROM DATABASE;
if ($status == 'disable') { 
  echo 'THIS WEBPAGE STATUS IS DISABLE';
} else { 
  echo 'THIS WEBPAGE STATUS IS ACTIVE';
}

Could some one help me with this, please? It looks very east from what I have seen, but just not for me, because I am new with this JSON.

Thanks a lot

3
  • What exactly do you need help with? I.e. are you getting the wrong result? What do you think it ought to be? Commented Jul 20, 2015 at 15:44
  • In API response what exactly you need to show and you want to manage the response using javascript? Commented Jul 20, 2015 at 15:55
  • 1
    You should take a look at PHP json_encode() and jQuery AJAX, then provide a more specific question on what you need help with. Commented Jul 20, 2015 at 16:36

2 Answers 2

1

Simply use json_enconde() as @Siphon said.

If that doesnt work try to do this:

<?php
   echo "<pre>";
   print_r($status);
   echo "</pre>";
?>

Check what that says, if something like this shows up:

$data = RECORD FROM DATABASE;



 Array $data(
        ['status'] => 'enabled',
    );

if that shows up then your if statment is wrong and you need to do this:

$data= RECORD FROM DATABASE;
if ($data['status'] == 'disable') { 
  echo 'THIS WEBPAGE STATUS IS DISABLE';
} else { 
  echo 'THIS WEBPAGE STATUS IS ACTIVE';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that. But my problem isnt about making php code part, my problem is about making json code... I will try to write more about that.. I would like help with making that part which is on online server X where are json code and some php code, like DB with websites (Y, Z) and its status... and into these websites (Y, Z) I check if into server X website status is disable or active. something like this youtube.com/watch?v=F5pXxS0y4bg but just with results for one website not all and dont show these results into consule but use it in website functions.
If you give me your email i can help you.
1

Your question is broad but here are some pointers:

you will need a query

SELECT * FROM `table` WHERE name = ?

Then you encode the data (array) to PDO using json_encode()

Example in PDO:

$data = $stmt->fetchAll();
$json = json_encode($data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.