while($ row = mysqli_fetch_array($ result))在php中的for循环内

时间:2018-10-14 16:22:52

标签: php mysql

我正在尝试将一个表中的值插入一些表中,表的数量是变化的,所以我尝试使用for循环并在其中使用while循环,该循环获取我必须从中插入的表的记录,但是while循环在for循环内显示警告“警告:mysqli_fetch_array()期望参数1为mysqli_result,布尔值在...中给出”

我的代码是:

<?php
$db_connection=mysqli_connect("localhost: 3306","root","root","ex1");
$room="select * from class;";
$room=mysqli_query($db_connection,$room);
$num=0;
$row=array();
while($room_row=mysqli_fetch_array($room))
{
     $result=mysqli_query($db_connection,"create table $room_row[0](usn varchar(12),sem int);");
     $row[$num]=$room_row[0];
     $num++;
}
$n=$num;
$num=0;
$sem_3=mysqli_query($db_connection,"select usn from student where sem=3;");
$sem_5=mysqli_query($db_connection,"select usn from student where sem=5;");
$sem_7=mysqli_query($db_connection,"select usn from student where sem=7;");
$sem_3=mysqli_fetch_array($sem_3);
$sem_5=mysqli_fetch_array($sem_5);
$sem_7=mysqli_fetch_array($sem_7);
$sem_3=$sem_3[0];
$sem_5=$sem_5[0];
$sem_7=$sem_7[0];
for($num=1;$num<$n;$num++)
{
     $room="select room_capacity from class where room_no=$row[0];";
     $room=mysqli_query($db_connection,$room);
     $room_row=mysqli_fetch_array($room);
     $each_sem_student_capacity=(int)$room_row[0]/3;
     $sem_3=mysqli_query($db_connection,"select usn,sem from student where sem=3 and usn>=$sem_3 limit $each_sem_student_capacity;");
     $sem_5=mysqli_query($db_connection,"select usn,sem from student where sem=5 and usn>=$sem_5 limit $each_sem_student_capacity;");
     $sem_7=mysqli_query($db_connection,"select usn,sem from student where sem=7 and usn>=$sem_7 limit $each_sem_student_capacity;");
     while($sem_3rd=mysqli_fetch_array($sem_3)) // warning is showing here
     {
          mysqli_query($db_connection,"insert into $room_row[0] values($sem_3rd[0],$sem_3rd[1]);");
     }
     while($sem_5th=mysqli_fetch_array($sem_5)) // warning is showing here
     {
          mysqli_query($db_connection,"insert into $room_row[0] values($sem_5th[0],$sem_5th[1]);");
     }
     while($sem_7th=mysqli_fetch_array($sem_7)) // warning is showing here
     {
          mysqli_query($db_connection,"insert into $room_row[0] values($sem_7th[0],$sem_7th[1]);");
     }
     $sem_3=$sem_3rd[0];
     $sem_5=$sem_5th[0];
     $sem_7=$sem_7th[0];
}
?>

1 个答案:

答案 0 :(得分:0)

替换

$room="select * from class;";

使用

$room="select * from class";

由于您的SQL中有;,因此无法正常工作。