json php query with duplicate results

2

The result of my php query is shown as follows:

the data is repeated in each query.

            <?php
            header('Content-type: text/html; charset=UTF-8');
            include('functions.php');
            $sql = "SELECT * FROM 'mitabla'";
            function connectDB()
            {
                $server = "mihost";
                $user   = "miusr";
                $pass   = "mipass";
                $bd     = "mibd";
                $conexion = mysqli_connect($server, $user, $pass, $bd);
                mysqli_set_charset($enlace, "utf8");
                if ($conexion) {
                } else {
                }
                return $conexion;
            }
            function disconnectDB($conexion)
            {
                $close = mysqli_close($conexion);
                if ($close) {
                } else {
                }
                return $close;
            }
            function getArraySQL($sql)
            {
                $conexion = connectDB();
                mysqli_set_charset($conexion, "utf8"); 
                if (!$result = mysqli_query($conexion, $sql))
                    die(); 
                $rawdata = array(); 

                $i = 0;
                while ($row = mysqli_fetch_array($result)) {
                     $rawdata[$i] = $row;
                     $i++;
                }

                disconnectDB($conexion); 
                return $rawdata; 
            }
            $myArray = getArraySQL($sql);
            echo json_encode($myArray, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | WITHOUT_ARRAY_WRAPPER),"\n";
            ?>
    
asked by mono101 25.09.2018 в 18:54
source

1 answer

3

The quid of the issue is in the method definition < em> mysqli_fetch_array :

  

Get a row of results as an associative, numeric array, or   both .

The or both , which I intentionally put in bold, means that if you do not indicate the type of array you want, the function will assume that you want a style MYSQLI_BOTH (default value) which will create an associative as well as a numeric array. That's why your data is duplicated.

Duplicity will disappear when you indicate how you want the array. This can be understood by reading the section resulttype in the Manual:

  

resulttype

     

This optional parameter is a constant that indicates what type of array should be generated with the row information   current. The possible values for this parameter are the constants    MYSQLI_ASSOC , MYSQLI_NUM , or MYSQLI_BOTH .

Solution

You can then indicate how you want your data.

  • Associative arrangement:

            while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
                 $rawdata[] = $row;
            }
    

    Or:

    Since the Manual says that:

      

    By using the constant MYSQLI_ASSOC this function will behave identically to mysqli_fetch_assoc()

    We can do it in the following way, using this function:

            while ($row = mysqli_fetch_assoc($result)) {
                 $rawdata[] = $row;
            }
    
  • Numerical arrangement:

            while ($row = mysqli_fetch_array($result,MYSQLI_NUM)) {
                 $rawdata[] = $row;
            }
    

    Or:

    Since the Manual says that:

      

    ... with MYSQLI_NUM will behave exactly like the function mysqli_fetch_row()

    We can do it in the following way, using this function:

            while ($row = mysqli_fetch_row($result)) {
                 $rawdata[] = $row;
            }
    
  

NOTE: It is not necessary to implement a $i counter, as you were doing at the beginning. The array will be created alone, without need   of that counter.

    
answered by 25.09.2018 / 20:26
source