0

I am studying android. And I have database in MySQL , I want to transfer some data from database to android to show it like ListView. I have learned that I need PHP , and JSONArray. And that is my colums in MySQL: enter image description here

My code in PHP :

  $query = mysqli_query($con,
           "SELECT * FROM market ORDER BY id ");
  $someArray = [];

  while ($row = mysqli_fetch_assoc($query)) {
    array_push($someArray, [
'id' => $row['id'],
'nick' => $row['nick'],
'percent' => $row['percent'],
'minimum' =>$row['minimum'],
'logo_small' => $row['logo_small'],
'logo_big'  => $row['logo_big'],
'name' =>$row['name'],
'phone'  => $row['phone'],
'position'=> $row['position']
 ]);
  }
      echo json_encode($someArray);

AND It works! what i get : enter image description here BUT when I want to add (or write it instead of getting name or nick or etc.)

'adresses' => $row['adresses']

complete code when add this:

<?php

    $con = mysqli_connect(".....", ".....", ".....", "......."); //private

  $query = mysqli_query($con,
           "SELECT * FROM market ORDER BY id ");
  $someArray = [];

  while ($row = mysqli_fetch_assoc($query)) {
    array_push($someArray, [
'id' => $row['id'],
'nick' => $row['nick'],
'percent' => $row['percent'],
'minimum' =>$row['minimum'],
'logo_small' => $row['logo_small'],
'logo_big'  => $row['logo_big'],
'name' =>$row['name'],
'phone'  => $row['phone'],
'position'=> $row['position'],
'adresses'=>$row['adresses']
 ]);
  }
      echo json_encode($someArray);
?>

or

'description'=> $row['description']

It is not working... when i add these lines(or one of them) i get this (empty): enter image description here

Data in db for adresses and description(i have only 4 titles) enter image description here I can't get adresses and description. What is a problem? maybe in database?

Reason: Because of the special symbols(é, ü, ç) in db there was an error.

  • _"It is not working"_ means what? they are empty? It throws an error (what error?)? – Jeff Aug 04 '18 at 11:53
  • @Jeff they are not empty. just a second i will upload screen – Java Rzayev Aug 04 '18 at 11:54
  • @Jeff , i have upload screenshots. – Java Rzayev Aug 04 '18 at 12:00
  • looks like a php fatal error that is not shown. PLease show the complete syntax when you "add" `'adresses' => $row['adresses']` – Jeff Aug 04 '18 at 12:10
  • @Jeff , i have added complete code , look please... – Java Rzayev Aug 04 '18 at 12:17
  • Why not just use `$someArray[] = $row;` instead of all the individual assignments? – Nick Aug 04 '18 at 12:24
  • @JavaRzayev address or description may be having quotes, please check data in both of columns once – PravinS Aug 04 '18 at 12:27
  • @Nick but that code is working for not adresses and description, so why doesn't it work for adresses and description? – Java Rzayev Aug 04 '18 at 12:30
  • 1
    I can't see any issue with your code as it is. So I would think that the `json_encode` is failing, probably due to the values in the addresses and/or description columns. Can you check the result of `json_encode` and if it is `false`, echo the output of `json_last_error_msg()` – Nick Aug 04 '18 at 12:34
  • @PravinS I have uploaded screenshot of data from db. – Java Rzayev Aug 04 '18 at 12:35
  • 1
    I guess it's the special characters like `é` causing the problem. Make sure your script is utf-8, your database, .. related: https://stackoverflow.com/questions/16498286/why-does-the-php-json-encode-function-convert-utf-8-strings-to-hexadecimal-entit and https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Jeff Aug 04 '18 at 12:43
  • @Jeff You are right! Thank you very much! – Java Rzayev Aug 04 '18 at 12:53
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Aug 04 '18 at 15:45

0 Answers0