0

I'm having trouble iterating through MySQL rows. This is my current code:

$query = "SELECT * FROM translations
          WHERE iddoc = '$id'
          AND submitted = 1;";
$result= mysqli_query($query);
$numrows = mysqli_num_rows($result);
$row2 = mysql_fetch_row($result);

if ($numrows > 0) {
    while($eachrow = mysqli_fetch_array($result, MYSQLI_NUM)) {
        echo $eachrow[0];
        echo ", ";
        echo $numrows;
   }
}

The result of this is:

6, 2

But if there are 2 rows, why is the while loop ending after only 1 iteration? What am I understanding wrong?

EDIT: It appears to be displaying ONE LESS than the correct amount of rows. I.E. the while loop is running 1 less time than it should.

16
  • 2
    What is $numtrans? And I'm assuming you left out your select query for a reason... Commented Aug 22, 2012 at 18:58
  • You sure there is only one row? did you run it in a mysql broswer? Commented Aug 22, 2012 at 18:59
  • The code as it is has absolutely no problem! Add more significant code Commented Aug 22, 2012 at 19:00
  • 1
    You're not using mysqli_query correctly here. You should NEVER introduce things like $id directly in the query but should instead use ? placeholders and bind these values before executing the query. Failing to do this could lead to serious consequences. Commented Aug 22, 2012 at 19:19
  • 1
    @Hubrid What tadman meant was to use the prepare statement, not just putting in the whole query. Commented Aug 22, 2012 at 19:22

1 Answer 1

3

Found problem. I was fetching the first row with

$row2 = mysql_fetch_row($result);

outside of the while loop, thus causing it to start on the second row and skip over the first.

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

1 Comment

@Hubrid... Look it up sometime.

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.