1

I am doing a MySQL SELECT on my database with PHP and I want to loop through the results. I am using mysql_fetch_array() to do this. I was originally using a while loop to loop through the results the problem I encountered is that in the loop I need to get what row the loop is currently in. I thought a for loop would do this because then I would have $i to get the value of the problem is that I do not think it will work. Below is my code. Is it possible to do what I am asking and am I doing it the right way?

$q = "SELECT test_id, title, subject, type, creation_date FROM tests WHERE user_id='$user_id' LIMIT 10"; //select first ten of users tests
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

if (mysqli_affected_rows($dbc) > 0) {//if the query ran correctly and the test details were gathered from the database

$row = mysqli_fetch_array($r, MYSQLI_ASSOC)

for($i=1; i<10; i++) {

$test_id = $row['test_id'];
$test_type = $row['type'];
$creation_date = $row['creation_date'];
$creator = $user_id;
$title = $row['title'];
$subject = $row['subject'];

$q = "SELECT tag_id FROM test_tags WHERE test_id='$test_id[$i]"; //select tags corresponding to this test
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

}

2 Answers 2

5

Use the while loop like you did before and just keep a variable $i which is incremented once per iteration.

$q = "SELECT test_id, title, subject, type, creation_date FROM tests WHERE user_id='$user_id' LIMIT 10"; //select first ten of users tests
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

if (mysqli_affected_rows($dbc) > 0) {//if the query ran correctly and the test details were gathered from the database

    $row = mysqli_fetch_array($r, MYSQLI_ASSOC)
    $i = 0;

    while ( $row = mysqli_fetch_array($r, MYSQLI_ASSOC) ) {
        $test_id = $row['test_id'];
        $test_type = $row['type'];
        $creation_date = $row['creation_date'];
        $creator = $user_id;
        $title = $row['title'];
        $subject = $row['subject'];

        $q = "SELECT tag_id FROM test_tags WHERE test_id='$test_id[$i]"; //select tags corresponding to this test
        $r2 = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

        $i += 1;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Why is there a fetching line of code before the loop that does the same action?
0

I would use the foreach() construction to loop through the result object. Something like this:

//select first ten of users tests
$q = "SELECT test_id, title, subject, type, creation_date FROM tests WHERE user_id='$user_id' LIMIT 10"; 
$r = mysqli_query($dbc, $q);
$i = 0;
//loop through result object:
foreach ($r as $row) {
  $row[$i]['test_id'] = $test_id;
  //...

  $q = "SELECT tag_id FROM test_tags WHERE test_id='$test_id[$i]"; //select tags corresponding to this test
  $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

  //loop through the new result:

  foreach ($r as $tag) {
    $tags[] = $tag;
  }

  $i++; //increment counter.

  //Not sure where you're going from here, but...

  $row[$i]['tags'] = $tag; //add tags array to $row
  return $row[$i];
}

Comments

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.