problem when creating JSON

1

very good afternoon, community is passing me that when creating a json this brings me the duplicate data in the following way

in the json we see twice the same information duplicate and only need the one that has the names of the fields if suddenly someone knows the solution thank you very much ... I do not know what else is needed to help me since I am new commenting on this type of problems, thanks I'm working with vue,

How are you? Of course if you look

this is the function or method that makes the request to the DB

and this is the function in which I capture the returned response and store it in a model or variable type array

Cedano Thanks, of course, you are right not to pass a parameter in the query, the default process was to bring all the information with an index by the fetch filter that it has by default. Apply your method and it works perfectly, now I have a question with your explanation, regarding this point since I implemented it and it does not work $ this-> db_conexon-> setAttribute (PDO :: ATTR_DEFAULT_FETCH_MODE, PDO :: FETCH_ASSOC); I'm doing it this way

the google console shows the following

but I think it is referring to the empty json if you suddenly know the reason why the implementation of that maner does not work I thank you because I would like to have it global to avoid redundancy

    
asked by HB-DACO 30.11.2018 в 20:52
source

2 answers

0

I welcome Stackoverflow and I will explain the cause of the problem and propose a solution.

Cause of the problem

The reason you duplicate the data is because you are reading the results with the fetch_style by default of the fetch method that is PDO::FETCH_BOTH .

As the PHP Manual says:

  

PDO::FETCH_BOTH ( default ): returns an indexed array   both by column name, and numerically with base index 0   as it was returned in the result set. *

It's exactly what happens in your results, you have the data twice, one with an array of numerical index and another with an associative array.

Solution

Use a more specific fetch_style . For these cases, PDO::FETCH_ASSOC is usually used:

  

PDO::FETCH_ASSOC : returns an array indexed by the names of the   columns of the result set. *

You just have to pass the fetch_style as follows:

while ($dataCl=$datosC1->fetch(PDO::FETCH_ASSOC)){
    $all_data[]=$dataCl;
}

Et ... voilà.

Additional note

The fetch_style by default can be changed. For example if you are always going to use PDO::FETCH_ASSOC you can pass to the constructor an array of options, where you indicate the default mode:

$pdoOptions=array (
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                  )

This is the recommended way, to put other important configurations in this array, such as setting the character set, deactivating the emulated preparations, setting the exception handling.

Or you can do with setAttribute , once the connection is created:

$this->db_conexon->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

If you do this before then you can use fetch() as you have it in your code and it will not duplicate the results.

* See the fetch_style section in the PHP Manual .

    
answered by 01.12.2018 / 03:03
source
0

The problem is in your query within the function sucursalesCLientes($id) when in the variable $query you do not assign the id , you are only looking for all the records without filter, assign the id that you receive as a parameter in the function in this way:

$query = "SELECT * FROM cliente_sucursal WHERE Nit_cliente='".$id."'"; 

(remember not to place images of the code but the code in text to facilitate the answers):

    
answered by 30.11.2018 в 21:46