Hello. There was such a problem.

$connection = new mysqli("localhost", "root", "root", "main"); mysqli_set_charset($connection, "utf8"); if ($result = mysqli_query($connection, "SELECT DISTINCT manufacturer FROM oc_1auto order by 1;")) { while($row = $result->fetch_array(MYSQL_ASSOC)) { $myArray[] = $row; } echo json_encode($myArray); } $result->close(); 

This code performs well, the data is displayed on the client side.

I want to include the file to connect to the database.

 header('Content-Type: application/json'); include_once('./db_mysqli.php'); if ($result = mysqli_query($connection, "SELECT DISTINCT manufacturer FROM oc_1auto order by 1;")) { while($row = $result->fetch_array(MYSQL_ASSOC)) { $myArray[] = $row; } echo json_encode($myArray); } $result->close(); 

And nothing happens. Data does not come in json format.

Tell me where to look?

  • the difference between the 1st and second blocks is only in include_once. Therefore, the logical question is what inside this include_once? The second point is that if the error is here json_encode , then json_last_error() will help you to find out what is wrong inside and why it does not turn it. - alexoander
  • include_once ('./ db_mysqli.php'); $ connection = new mysqli ("localhost", "root", "root", "main"); mysqli_set_charset ($ connection, "utf8"); - matiush1785
  • and if at all comes? I just read about php that include_once will transfer and immediately compile the code, but I would not be so sure that $ connection does not throw an error like "unknown variable - declaration is required". Well, I have already described another solution above - see what happens in json-encode. mb you receive an empty answer or an answer that does not parse - alexoander

0