json_encode doesn't output in Json format? [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
So, I'm trying to make a simple web JSON API with data from my MySQL DB (10.0.35-MariaDB) I'm trying to use json_encode(); to output the information. It succeeds in gathering my data and outputting it only not in a JSON format?
Here is what's outputted compared to what I'm expecting;
What I have...
What I dream about having...
I think the fact that the JSON Formatter Chrome extension doesn't even pick up on my page sorta is a blatant sign that I'm not doing something right...
$conn = mysqli_connect('host', 'username', 'pass', 'db');
$query = mysqli_query($conn, "SELECT * FROM `table`;");
while ($row = mysqli_fetch_assoc($query)) {
$data[] = $row;
}
echo json_encode($data);
I know that to archive such a beautiful JSON file like steam's it'll require more work so for now, I'm just asking for help on actually outputting in JSON format.

What is being output is perfectly fine JSON. Your browser extension simply doesn't pick up on that fact because you're most likely not denoting the content type in the HTTP Content-Type header. Without that, anything is simply plaintext to the browser. Add this before outputting your JSON:
header('Content-Type: application/json');

Related

Why passing variables through url in php not working? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to pass some variables through url in php. Im using the following script
$query1 = new ParseQuery("Drivers");
$query1->includeKey("driverUserId");
$query1->descending("createdAt");
$results1 = $query1->find();
$object1 = $results1[$i];
$uname=$object1->get('driverUserId');
echo ''.$uname->get('name').'';
echo '<td>'.$uname->get('name').'</td>';
echo '<td>'.$uname->get('username').'</td>';
But when I execute the page, script stopped working in this line.
echo ''.$uname->get('name').'';
When I removed '.$uname.' it worked fine.
Because if $uname has a ->get() method, it's obviously a class or an object and you can't directly use it like a string (if it hasn't any tostring modifiers). Try to write it like this (just guessing):
echo ''.$uname->get('name').'';
Use the below code:
$id=$uname->get('id');
$name=$uname->get('name');
echo ''.$name.'';

Echo steam avatar and steam name in php & html code without login [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to make a code who echo a users name and his avatar from steam, but i dont know how, i tryied this: http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=52A66B13219F645834149F1A1180770A&steamids=76561197960435530 but i dont know how to get just the username and the avatar from that info im getting, any help?
You must get the content of this file through file_get_contents and parse that json format to php array. After just get element personaname and avatar and use it with your HTML code
An example of how you could parse that json.
$json = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=52A66B13219F645834149F1A1180770A&steamids=76561197960435530');
$parsed = json_decode($json);
foreach($parsed->response->players as $player){
echo $player->personaname . '<br>';
echo "<img src='" . $player->avatar . "'>";
}
If you have any questions, feel free to ask!

PHP function Not giving any result [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am facing some problem in my PHP function.
function pagename($id){
$query=mysql_query("select * from tbl_pages where recid='$id' and langid='$LangID'")
$rs=mysql_fetch_array($query);
$page_name=$rsp['pgname'];
print $page_name;
}
i am not getting any resutl
I saw some problems in your code
First, there is no connection string to your database, I hope you do the connection before to do the request.
Then, in your request, you try to use a variable $LangID not declared in your function, maybe you forgot to put it in your function declaration.
You put the result of your request in the $rs variable and then you try to read the $rsp variable.
You are using mysql in your code, actually it's very unsafe to use it, it's very recommended to use mysqli or PDO instead.
Finally, you don't return anything with your function, you are missing the return statement or maybe you just want to display the result ?
EDIT : I suggest you to write your SQL requests with uppercase, it's more readable for you and other people who read your code.
SELECT * FROM tbl_pages WHERE recid='$id' AND langid='$LangID'
There's no $LangID parameter.
$rs and $rsp are different variables in your code, you should use only one of them.
This function does not return anything, even if you get something in your print.
Check if mysql connection is already established.

PHP reading through json [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
We have created some json as follows
{"deliveries":[{"cn":"1498","airlinetype":" Airbus A330-200 ","registration":" G-VYGK ","airline":"Thomas Cook Airlines","date":" 29. Apr 2015 "}]}
We are trying to loop through each delivery extracting the data to insert into the database. We ran the data through a json validator and it says everything is OK. Do we need to use json_encode to convert this as we are having problems trying to access each of the elements.
What would be the best approach to do this?
Thanks in advance
Rich
You should use json_decode() function. Please check solution below:
$json = '{"deliveries":[{"cn":"1498","airlinetype":" Airbus A330-200","registration":" G-VYGK ","airline":"Thomas Cook Airlines","date":" 29. Apr 2015 "}]}';
$data = json_decode($json);
foreach($data->deliveries[0] as $field=>$value) {
echo "{$field} = {$value}<br>";
//*** code to write to db follows here...
}
You want to decode the json string that you have posted.
A quick way since you know the name of each json branch is the following
$json = '{"deliveries":[{"cn":"1498","airlinetype":" Airbus A330-200 ","registration":" G-VYGK ","airline":"Thomas Cook Airlines","date":" 29. Apr 2015 "}]}';
$out = json_decode($json,true);
foreach($out["deliveries"] as $deliveries) {
echo $deliveries["cn"];
echo $deliveries["airlinetype"];
echo $deliveries["registration"];
echo $deliveries["airline"];
echo $deliveries["date"];
}
Check it out: http://sandbox.onlinephpfunctions.com/code/cfd8cdeb7f83536db4b4df74778243306c1c2d8c

Why convert IP Address to Hostname by used "gethostbyaddr" but don't work? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I try to convert IP Address to Hostname by used "gethostbyaddr" but don't work. I use file.text that is log file to analyze which page that user used a lot or less. So I try to cut sentence that use array. This is my code.
$file=fopen("log.txt","r");
while (!feof($file)) {
$buffer = fgets($file, 4096);
$text= explode(" ",$buffer,10);
$text_2=#$text[1];
$ip=substr($text_2,1,11);
$ip2=gethostbyaddr($ip);
echo"INSERT INTO `log` VALUES ('$ip2');"."<br>"; //Show results
//echo"<br>"; //New line
}
fclose($file);
Thanks
that means gethostbyaddr failed.
not because your input, find the explanation in php documentation
http://php.net/manual/en/function.gethostbyaddr.php
"Returns the host name on success, the unmodified ip_address on failure, or FALSE on malformed input."

Categories