未定义的偏移量:循环PHP为1

未定义的偏移量:循环PHP为1,php,Php,它正在获取的数组大小为2,因此如果我们注释掉$size,它将返回2,但for循环只打印表列第0个位置的值,而不打印第2个位置的值,从而给出错误: Undefined offset: 1 代码: $allunstitched="Select p_id from unstitchedproduct"; $resultAllUnstitched= mysqli_query($connection,$allunstitched); $AllUnstitchedResult= mysqli_fetch_

它正在获取的数组大小为2,因此如果我们注释掉
$size
,它将返回2,但for循环只打印表列第0个位置的值,而不打印第2个位置的值,从而给出错误:

Undefined offset: 1
代码:

$allunstitched="Select p_id from unstitchedproduct";
$resultAllUnstitched= mysqli_query($connection,$allunstitched);
$AllUnstitchedResult= mysqli_fetch_array($resultAllUnstitched);
//$size=count($AllUnstitchedResult);

for ($i=0, $count = count($AllUnstitchedResult); $i < $count; ++$i) {
  print $AllUnstitchedResult[$i];
}
$allunstitched=“从unstitchedproduct中选择p_id”;
$resultAllUnstitched=mysqli\u查询($connection,$allunstitched);
$AllUnstitchedResult=mysqli\u fetch\u数组($resultAllUnstitched);
//$size=计数($AllUnstitchedResult);
对于($i=0,$count=count($AllUnstitchedResult);$i<$count;++$i){
打印$AllUnstitchedResult[$i];
}

如果从0开始,如果count=5,则将有6个元素(0,1,2,3,4,5),请注意这一点。您需要循环(
count-1
)次:

$count=count($AllUnstitchedResult);
对于($i=0,$i<$count-1;$i++){
打印$AllUnstitchedResult[$i];
}

嗯,我认为你的for循环没有问题;由于您的查询读取一个字段/列,因此它应该有1迭代(=0索引),因此未定义的=1是正确的(在我看来)

您应该做的是重复调用
mysqli\u fetch\u array
以获取所有行的循环,如下所示:

while($AllUnstitchedResult = mysqli_fetch_array($resultAllUnstitched,MYSQLI_NUM)) {
   foreach ($AllUnstitchedResult as $field) {
      print $field;
   }
}

您正在使用mysqli\u fetch\u数组。默认情况下,此函数返回结果两次:按数字和列名索引。在这里,结果集只有一列,因此您在数组中获得它的值两次:首先使用索引0,然后使用字符串索引。没有索引1

要仅使用数字索引获取结果,请将MYSQLI_NUM作为第二个参数传递给MYSQLI_fetch_数组:

mysqli_fetch_array($resultAllUnstitched, MYSQLI_NUM);

现在我在数据库中有2个值,所以count是2,$I=0,所以循环正常!这样就不会处理数组中的最后一项。
mysqli_fetch_array($resultAllUnstitched, MYSQLI_NUM);