Search code examples
phparraysmysqliresultset

How to get all values from resultset?


I'm trying to get all the values from the table and then assign them to a variable, there are only 2 options in the table so it can have 1 or 2 values, but when trying this it only gives back when first number (1) and the second ($fila2 = $rs2[1];) it says that is a Undefined offset

include('conexion.php');
$str_query2 = "SELECT gc_id FROM prod_grupocompl where pr_id='171' "; //  Variable que guarda la consulta a efectuar
$query2 = mysqli_query($conexion,$str_query2) or die (mysql_error()); // EjecuciÛn de la consulta y se guarda el resultado en $query
$rs2 = mysqli_fetch_array($query2);
$fila = $rs2[0];
$fila2 = $rs2[1];
echo $fila;
echo $fila2; 

Solution

  • The resultset will be returning two rows not two fields in one row.

    To access the second row, I recommend a loop.

    while($row=mysqli_fetch_array($query2, MYSQLI_NUM)){
        // now you can access the field value like this for each row...
        $fila=$row[0];  // do what you want with each value, echo or something.
    }
    

    p.s. mysql_error() is missing an i.