1
I wonder if anyone finds any error in my code because I’m trying to create a Json with PHP but it only returns NULL
.
My Host server uses PHP 5.6
<?PHP
$host = "...";
$usuario = "...";
$senha = "...";
$banco = "...";
$database = mysqli_connect($host,$usuario,$senha,$banco);
$result=mysqli_query("SELECT * FROM tbconsultas");
$i=0;
while($row=mysqli_fetch_array($result)) {
$response[$i]['url'] = $row['url'];
$response[$i]['title']= $row['title'];
$data['posts'][$i] = $response[$i];
$i=$i+1;
}
$json_string = json_encode($data);
$file = 'file.json';
file_put_contents($file, $json_string);
?>
Why should we not use mysql type functions_*?
– MarceloBoni
@Marceloboni I tried to use 'mysqli' but created Null the same way
– Matheus Rohwedder
Never use the mysql extension, the extension is obsolete and no longer works from php 7
– MarceloBoni
I am using php 5.6 on my @Marceloboni server
– Matheus Rohwedder
put at the beginning of your code
error_reporting(E_ALL);
See if she gave a message and put it here to help.– claudsan
First of all, change the extension to
mysqli
;] It is not because your server still supports the extension, that it should still be used– MarceloBoni
I switched to the
mysqli
however I did not succeed in creating Json @Marceloboni– Matheus Rohwedder
What returns
null
? Perform avar_dump( json_last_error_msg());
and check if there is an error (and which is it) in json_encode. See if the database is in UTF8 as well.– Inkeliz
@Inkeliz the
null
is the Json I get after creating it with php. andvar_dump
I should put it where ?– Matheus Rohwedder
Place at the end of the code. The
json_last_error_msg()
will return the code of the last error that occurred onjson_encode()
.– Inkeliz
@Inkeliz
string(8) "No error"
that was the mistake I received– Matheus Rohwedder
@Matheusrohwedder, you put the
json_last_error()
after the call ofjson_encode()
? This could be something with UTF8 since it’s picking up Urls and titles.– user13603
@Fernandobagno yes it was after the
json_encode()
I’ll have to take a look but I think this in utf-8 yes– Matheus Rohwedder
It would be nice to see what your base has, because your code ran here for me quietly using a base of mine.
– user13603
@Fernandobagno by what I saw my phpmyadmin is using utf8mb4_unicode_ci
– Matheus Rohwedder
@Fernandobagno do not know what happened but mysteriously worked
– Matheus Rohwedder
Have you changed the Internet? Are you using UTF8? Ideally your bank is in Utf8_general_c
– user13603
@Fernandobagno I implemented Anderson’s code below and it worked now
– Matheus Rohwedder