mysqli and php query does not work

1

I want to show a certain number of "products" on the screen, I create a series of bootstrap letters to store the information there. I generate my connection (I clarify that the connection is perfect):

<?php
   $conexion = mysqli_connect("localhost", "user", "**", "pampa");
   mysqli_select_db("pampa",$conexion);
?>

and here the query I want to show:

<?php  

                $consulta = "SELECT * FROM productos";
                if ($resultado = $conexion->query($consulta)) {
                    /* obtener el array de objetos */
                    while ($fila = $resultado->fetch_row()) {
                                        echo '<div class="carta">';  
                                        echo '<div class="sugerencias _hot"><span class="hot">HOT</span></div>';
                                        echo '<div class="imagenCarta">';
                                        echo '<img class="imgCARD" src='.$fila[imagen].'>';
                                        echo '</div>';
                                        echo '<div class="cuerpo">';
                                        echo '<div class="titulo">'.$fila[titulo].'</div>';
                                        echo '<div class="info">'.$fila[descripcion].'</div>';
                                        echo '<div class="rec">Por dia: <span style="color:#39b540;font-size:15px;font-weight: 300;">'.$fila[precio].'</span></div>';
                                        echo '</div></div>'; 
                    }

                    /* liberar el conjunto de resultados */
                    $resultado->close();
                }
               ?>

The problem is that the result you show me is:

    
asked by Tomas Francisco Firbeda 17.04.2018 в 06:27
source

3 answers

1

You could check how is the order of your columns, to know how to show the data, if the order was image, title and description you could access them as well;

 echo '<img class="imgCARD" src='.$fila[0].'>';
 echo '<div class="titulo">'.$fila[1].'</div>';
 echo '<div class="info">'.$fila[2].'</div>';

You could also make var_dump of filas , so you'll know how the data is being returned.

I hope it's of your use.

    
answered by 18.04.2018 / 05:30
source
1

To work in the way you set it, in your cycle, instead of using the fetch_row () function, you should use fetch_assoc () to return a arrangement per row; in said arrangement the name of the field is associated with the index of an element of the array.

In my opinion, the way you are doing it is more practical because it makes your code more readable, and if you use it by numerical reference it is more difficult since you have to know the order of the fields based on the query that do.

More information about this feature in link

    
answered by 19.04.2018 в 15:25
0

Probably the variable $ row [image] is not returning what you expect. And without seeing the code or what the database returns, I imagine that this

echo '<img class="imgCARD" src='.$fila[imagen].'>';

It should be something like this

echo '<img class="imgCARD" src="path_to_img/'.$fila[imagen].'">';

I have only added the double quotes so that the src is quoted, apart from adding the path_to_img that will not be necessary if the variable $ file [image] returns /ruta/hasta/la/imagen.jpg. If that variable only returns "image.jpg", you will need to put the whole path to the folder that contains the image you want to show.

    
answered by 17.04.2018 в 11:00