SELECT in PHP only returns from the second INSERT of the same user

Asked

Viewed 25 times

0

The result of the following SELECT

    <?php
         include_once("conect.php");
         $id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_STRING);
         $result_usuario = "SELECT * FROM produ_usu WHERE id = '$id'";
         $resultado_usuario = mysqli_query($conn, $result_usuario);
         $row_usuario = mysqli_fetch_assoc($resultado_usuario);

          if(($resultado_usuario) AND ($resultado_usuario->num_rows != 0)){
               while($row_usuario = mysqli_fetch_assoc($resultado_usuario)){
     ?>

only returns data from the second INSERT with same id (user id).

I know the trouble is caused by the line

$row_usuario = mysqli_fetch_assoc($resultado_usuario);

just below the line

$resultado_usuario = mysqli_query($conn, $result_usuario);

Comment the line and SELECT returns after a user’s first INSERT with his id normally.

In short, I do not understand why with the infamous line only returns from the second insert

The question is: why does this line return only after the second Insert of the same id?

  • Already gave a var_dump($row_usuario); to make sure that all data is coming correctly?

1 answer

1


The mysqli uses a cursor to control the items you recover through mysqli_fetch_assoc that advances 1 item every time it is called, and as you have already given one mysqli_fetch_assoc there in $row_usuario = mysqli_fetch_assoc($resultado_usuario); , your while is starting on the next item.

This is best explained in php doc https://www.php.net/manual/en/mysqli-result.fetch-assoc.php

  • Putz, I saw that only returned when num_rows was 2, which would indicate exactly what you just answered. Congratulations on the clarity of your vision. I’ll consult my optician :-)

  • Dispose. Letting some detail go is something that even the most experienced developer lets happen ^^

  • It is true, but there are people there negativing my question, can not contribute but knows negativar, I’m sorry.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.