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

Php 如何使用for循环遍历mysql_fetch_array()?,php,mysql,loops,for-loop,Php,Mysql,Loops,For Loop,我正在用PHP对我的数据库执行MySQLSELECT,我想循环查看结果。我正在使用mysql\u fetch\u array()来实现这一点。我最初使用的是while循环遍历结果。我遇到的问题是,在循环中,我需要获取循环当前所在的行。我认为for循环可以做到这一点,因为这样我就有$I来获得问题的值,我认为它不起作用。下面是我的代码。有可能做到我所要求的吗?我做得对吗 $q = "SELECT test_id, title, subject, type, creation_date FROM te

我正在用PHP对我的数据库执行MySQL
SELECT
,我想循环查看结果。我正在使用
mysql\u fetch\u array()
来实现这一点。我最初使用的是
while
循环遍历结果。我遇到的问题是,在循环中,我需要获取循环当前所在的行。我认为for循环可以做到这一点,因为这样我就有$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)

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));

}
$q=“从测试中选择测试id、标题、主题、类型、创建日期,其中用户id=“$user\u id”限制为10”//选择前十个用户测试
$r=mysqli_query($dbc,$q)或trigger_error(“query:$q\n
MySQL error:“.mysqli_error($dbc)); if(mysqli_impact_rows($dbc)>0){//如果查询运行正确并且从数据库收集了测试详细信息 $row=mysqli\u fetch\u数组($r,mysqli\u ASSOC)
对于($i=1;i像以前一样使用
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;
    }
}
$q=“从测试中选择测试id、标题、主题、类型、创建日期,其中用户id=“$user\u id”限制10”;//选择前十个用户测试
$r=mysqli_query($dbc,$q)或trigger_error(“query:$q\n
MySQL error:“.mysqli_error($dbc)); if(mysqli_impact_rows($dbc)>0){//如果查询运行正确并且从数据库收集了测试详细信息 $row=mysqli\u fetch\u数组($r,mysqli\u ASSOC) $i=0; 而($row=mysqli\u fetch\u数组($r,mysqli\u ASSOC)){ $test_id=$row['test_id']; $test_type=$row['type']; $creation_date=$row['creation_date']; $creator=$user\u id; $title=$row['title']; $subject=$row['subject']; $q=“从test_标记中选择标记\u id,其中test_id='$test_id[$i]”;//选择与此测试对应的标记 $r2=mysqli_query($dbc,$q)或trigger_error(“query:$q\n
MySQL error:“.mysqli_error($dbc)); $i+=1; } }
像以前一样使用
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;
    }
}
$q=“从测试中选择测试id、标题、主题、类型、创建日期,其中用户id=“$user\u id”限制10”;//选择前十个用户测试
$r=mysqli_query($dbc,$q)或trigger_error(“query:$q\n
MySQL error:“.mysqli_error($dbc)); if(mysqli_impact_rows($dbc)>0){//如果查询运行正确并且从数据库收集了测试详细信息 $row=mysqli\u fetch\u数组($r,mysqli\u ASSOC) $i=0; 而($row=mysqli\u fetch\u数组($r,mysqli\u ASSOC)){ $test_id=$row['test_id']; $test_type=$row['type']; $creation_date=$row['creation_date']; $creator=$user\u id; $title=$row['title']; $subject=$row['subject']; $q=“从test_标记中选择标记\u id,其中test_id='$test_id[$i]”;//选择与此测试对应的标记 $r2=mysqli_query($dbc,$q)或trigger_error(“query:$q\n
MySQL error:“.mysqli_error($dbc)); $i+=1; } }
我将使用
foreach()
构造来循环遍历结果对象。类似如下:

//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];
}
//选择前十个用户测试
$q=“从测试中选择测试id、标题、主题、类型、创建日期,其中用户id=“$user\u id”限制为10”;
$r=mysqli_查询($dbc,$q);
$i=0;
//循环遍历结果对象:
foreach($r作为$row){
$row[$i]['test\u id']=$test\u id;
//...
$q=“从test_标记中选择标记\u id,其中test_id='$test_id[$i]”;//选择与此测试对应的标记
$r=mysqli_query($dbc,$q)或trigger_error(“query:$q\n
MySQL error:“.mysqli_error($dbc)); //循环浏览新结果: foreach($r作为$tag){ $tags[]=$tag; } $i++;//递增计数器。 //不知道你要从这里走到哪里,但是。。。 $row[$i]['tags']=$tag;//将标记数组添加到$row 返回$row[$i]; }
我将使用
foreach()
构造来循环遍历结果对象。类似如下:

//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];
}
//选择前十个用户测试
$q=“从测试中选择测试id、标题、主题、类型、创建日期,其中用户id=“$user\u id”限制为10”;
$r=mysqli_查询($dbc,$q);
$i=0;
//循环遍历结果对象:
foreach($r作为$row){
$row[$i]['test\u id']=$test\u id;
//...
$q=“从test_标记中选择标记\u id,其中test_id='$test_id[$i]”;//选择与此测试对应的标记
$r=mysqli_query($dbc,$q)或trigger_error(“query:$q\n
MySQL error:“.mysqli_error($dbc)); //循环浏览新结果: foreach($r作为$tag){ $tags[]=$tag; } $i++;//递增计数器。 //不知道你要从这里走到哪里,但是。。。 $row[$i]['tags']=$tag;//将标记数组添加到$row 返回$row[$i]; }
为什么在执行相同操作的循环之前有一行代码提取?为什么在执行相同操作的循环之前有一行代码提取?