1

I am trying to make a drop down list which will contain as many options as there are in the database table.

    $qry3 = "SELECT * FROM employees WHERE buss_id_fk = '{$_SESSION ['user_id']}' ";
    $result3 = mysqli_query ( $con, $qry3);

    while ($row4 = mysqli_fetch_assoc($result3)){ 
    echo $row4['emp_id'] . " " . $row4['username'] . '</br>';
    }

$qry3 is my query and it works fine with $row4 and everything shows as intended. Now when I move into my form where my drop down list should exist I made this code

 <!--the list of employees the business has -->
                  <select name="employees" class="form-control">

 <?php while ($row3 = mysqli_fetch_assoc($result3)) {

 echo "<option value='" .$row3['emp_id']. "'>" .$row3['username']. "</option>" ;

                  } ?>

                  </select> <br>

my drop down list shows zero results while it should show some, just like that $row4 did. Checked a similar question where there was a typo in the code, didn't help. Thanks!

4
  • 1
    Do you have both examples of code running at the same time? If so that would be the problem I think. When you loop through all of $result3 with $row4 you have moved the pointer in mysql to the end so when you get to the select ($row3) there are no more rows to loop through. Commented Mar 28, 2017 at 23:28
  • I made $row4 after $row3 didn't work actually, so I'm not sure why the pointer wasn't at the beginning of the values, and I'm not making any operations on that table before that code, I did make some SQL moves on other tables, though. Confusing why would this happen. Thanks for your comment! Commented Mar 28, 2017 at 23:34
  • 1
    WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST, $_GET or any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented Mar 28, 2017 at 23:42
  • oh dear lord, I meet this warning once again, I guess I should really check the prepared statements thingy out, the bind_param is a new one to me, though. Thanks for taking the time to warn me about this! Commented Mar 28, 2017 at 23:46

1 Answer 1

4

You need to reset the pointer back to the beginning of the result set if you wish to iterate through the results again:

<?php 
    mysqli_data_seek($result3, 0); // Zero indicates the beginning
    while ($row3 = mysqli_fetch_assoc($result3)) {

See the manual: mysqli_data_seek()

Sign up to request clarification or add additional context in comments.

7 Comments

Wow it worked. Why was this necessary though? may you please explain? I made the $row4 to test if the values are being retrieved from the table after my $row3 didn't work, so it shouldn't be about the $row4. And I never faced this issue before.
Because the data pointer is at the end of the result set so next time you try to access that result set it appears as though there are no more rows to iterate through. By resetting the pointer back to the beginning you have access to that result set again and can iterate through it once more.
Yeah I understand that, but I did not make any operations on that table before that $row3 code. And it didn't work, after that I made $row4 to test if the values are actually being pulled from the table or not. Now I removed $row4 and removed mysqli_data_seek() and it's working as intended... so weird.
Make sure you didn't iterate through that result set somewhere else. Like debugging code.
I did iterate through two other tables on this page before iterating through this table, maybe the pointer remained stuck somewhere from previous actions. Thanks a lot man, your answer solved the problem regardless.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.