简体   繁体   English

如何使用for循环遍历mysql_fetch_array()?

[英]How can you loop through a mysql_fetch_array() with a for loop?

I am doing a MySQL SELECT on my database with PHP and I want to loop through the results. 我正在使用PHP在数据库上执行MySQL SELECT ,我想遍历结果。 I am using mysql_fetch_array() to do this. 我正在使用mysql_fetch_array()来做到这一点。 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. 我最初是使用while循环遍历结果的,我遇到的问题是在循环中我需要获取循环当前所在的行。我认为for循环可以做到这一点,因为那时我将有$ i来获取问题的价值在于我认为这行不通。 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));

}

Use the while loop like you did before and just keep a variable $i which is incremented once per iteration. 像以前一样使用while循环,只保留变量$i ,该变量在每次迭代中增加一次。

$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;
    }
}

I would use the foreach() construction to loop through the result object. 我将使用foreach()构造来循环遍历结果对象。 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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM