This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
I'm trying to fetch data from a MariaDB database of festivals and respective locations. When running:
while($row = $sth->fetch_assoc()){}
and iterating over $row while outputting the values, I get data as it is in the database.
However, when storing each row like so:
while($row = $sth->fetch_assoc()){
$results[] = $row;
}
And echoing the results as JSON (echo json_encode($results);)
I get this:
{"id":"0","name":null,"village":"0","startDate":"2019-01-16",
"endDate":"2019-01-23","message":null}
This is for an existing Linux server, which I do not manage (I'm using CPanel). PHP version is 5.4 and MariaDB 10.1.37.
So far, a lot of code samples on Stack Overflow and other websites are using
$results[] = $row;
for storing the results.
I'm returning to PHP after 3 years of Swift only programming... So I suspect this could be a simple issue to solve...
Thanks!
May be I'm bit too late for answering this question, but it may help someone stuck with this issue. Recently I faced a similar issue and it took me 3 days to understand that it is an issue with the utf-8 characters. Data stored in database are perfectly fine. But when it comes to return it via json_encode(), it shows no data at all as json_encode only supports utf-8 data(reference).
For your case the following method should work-
foreach ($results as &$r) {
$r['name'] = utf8_encode($r['name']);
//same for all other items
}
Related
This question already has answers here:
How can I update code that uses the deprecated each() function?
(12 answers)
Closed 11 months ago.
I had some code that has stopped working. I suspect the web server hotel php version has been changed as perfectly good working code no longer works. This is the code. It read in the number of each category from "$catcho" the user chose and created an array of them all. So if the user chose category 1, 3 and 4 the array "$whiskers" would have the values 1, 3 and 4 in it.
while (list ($key, $val) = #each ($catcho)) {
$whiskers[] = "$val"; // This builds a list of chosen cats
}
I have simplified the code because in the final version I don't actually read in all values as they are dependent upon other choices.
What other php code can I put in there instead of mine?
Thanks for any help you are able to give me.
as 'each' is now removed from php 8+
you can rewrite the construct as
foreach ($catcho as $val){
$whiskers[] = $val;
}
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');
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have tried, checked many previous topics, and i cant find the way.
Parsing JSON with PHP
There is the answer, but im blind; actually, im a medimu PHP programmer, but im new with Json.
Here is my Json:
{"result":"true","disponible":{"aaa":"0.00001362","bbb":"0.000392","ccc":"0.00788523","ddd":"0.00004443","eee":"0.0001755","fff":"0.1755","ggg":"797.64618376"}}
My code:
$balances = json_encode(get_balances(),true);
print_r($balances);
The screen show my Json, so everything is ok here. Now, i want take the bolded values from the json and assign it to PHP variables.
$variable1 = $balances["disponible"]["bbb"];
$variable2 = $balances["disponible"]["ggg"];
echo "Valor 1: ".$variable1 ."<br>";
echo "Valor 2: ";$variable2 ;
But it dont work. I tried with many combinantions and nothing.
What im doing wrong?
Thanks a lot in advance. Im blocked with this.
Replace: $balances = json_encode(get_balances(),true); with: $balances = json_decode(get_balances(),true); if you are trying to get associative array.
This question already has answers here:
Parsing JSON with PHP
(6 answers)
Closed 7 years ago.
I know this one has been asked loads of times, so mods please do not close this off as no past answers have worked and I have searched already! So I'm using the HaveIBeenPwned API which outputs what is apparently JSON like so:
[
{
"Title":"000webhost"
,"Name":"000webhost"
,"Domain":"000webhost.com"
,"BreachDate":"2015-03-01"
,"AddedDate":"2015-10-26T23:35:45Z"
,"PwnCount":12345678
,"Description":"In approximately March 2015, the free web hosting provider 000webhost suffered a major data breach that exposed over 13 million customer records. The data was sold and traded before 000webhost was alerted in October. The breach included names, email addresses and plain text passwords."
,"DataClasses":[
"Email addresses"
,"IP addresses"
,"Names"
,"Passwords"
]
,"IsVerified":true
,"IsSensitive":false
,"LogoType":"png"
}
]
That is the output if I use the following:
echo $output;
json_decode($output) doesn't work. Using a foreach($output as $key) doesn't work, it says invalid argument if I use that. I have tried a normal for() loop and again no joy. What is wrong with this output? How do I get at the values within it? As you should be able to tell I'm using PHP.
Use json_decode($output, true);
Second parameter defines result data format - object (false, default) or associated array (true).
This question already has answers here:
Processing large JSON files in PHP
(7 answers)
Closed 8 years ago.
Some API is providing json file of 88MB. I need to store in some of its value in database.
$cURL="url here"
$str = file_get_contents($cURL);
$str = json_decode($str);
echo '<pre>';
print_r($str);
this code is not working because file size is too large. I need to convert that json file to a php array. I am not able to parse this file. Any help from anyone?
Quite complicated if the problem should be solved during the query. Caching the preliminary data on the filesystem then separate the values into chunks would be able to solve the issue.