Take a row of Mysql with PHP without while

1

Hi, if you could help me, I'm trying to take a row from a Mysql table, I do not need to go through the table I just want to take a single row.

$consulta_precio_seco="SELECT * FROM secos WHERE codigo=10" ;
$resultados_precio_seco=mysqli_query($conexion,$consulta_precio_seco);
$fila=mysqli_fetch_row($resultados_cargar);
echo $fila[0];

Someone help me please.

    
asked by Javier 22.07.2018 в 20:35
source

2 answers

1

Query should be as follows assuming you only want to get the values of each column but a single row

<?php

$consulta_precio_seco="SELECT * FROM mensajes" ;
$resultados_precio_seco=mysqli_query($conexion,$consulta_precio_seco);
$fila=mysqli_fetch_row($resultados_precio_seco);
//así si quieres que los resultados salgan concatenados
echo $fila[0].' '.$fila[1].' '.$fila[2];

If you want the results to be shown one per line

  $consulta_precio_seco="SELECT * FROM mensajes" ;
    $resultados_precio_seco=mysqli_query($conexion,$consulta_precio_seco);
    $fila=mysqli_fetch_row($resultados_precio_seco);
    echo $fila[0].PHP_EOL;
    echo $fila[1].PHP_EOL;
    echo $fila[2].PHP_EOL;

As you can see, the changes made are minimal, but they are the following ones

  
  • to the function mysqli_fetch_row() I pass the variable $resultados_precio_seco inside, because it is the one that contains the set of results that come directly from your query
  •   
  • The data that is being returned comes within an array so when you do the echo you must indicate the position of the clusters   you want to read, for example in the table id, name, email id is in the   position 0, name in position 1 and email in position 2 (only as   example)
  •   
        
    answered by 22.07.2018 / 20:46
    source
    3

    mysqli_fetch_row must receive as a parameter a set of results, which is obtained with mysqli_query .

    In your code that set of results is called $resultados_precio_seco , but you are passing another variable that is $resultados_cargar .

    To solve the problem it would be then:

    $fila=mysqli_fetch_row($resultados_precio_seco);
    echo $fila[0];
    
      

    NOTE ON OPTIMIZATION:

         

    If you're only interested in one row, maybe you should limit the   results to a row, so as not to overload the database and the   server bringing unnecessary data. You can therefore write the   query like this: SELECT * FROM secos WHERE codigo=10 LIMIT 1

      

    NOTE ON NAME CONVENTION

         

    It might be useful if you use less extensive variable names. While this is no difficulty for the operation of the code, it is at the time of reading, writing, analyzing, organizing, understanding. It is not the same to have a variable called $resultados_precio_seco if you call it for example $rsPrecioSeco or have $consulta_precio_seco instead of $sqlPrecioSeco .

        
    answered by 22.07.2018 в 20:44