PHP for 循环变量未分配
我有一个 for 循环,它将查询结果写入表中。我有一个变量 ($rID_s
),它是根据查询中的值分配的。由于某种原因,它省略了第一次迭代。我有一个用于查询总行数的变量,它正在分配正确的数字。我尝试设置 $i = 0
和 $i = 1
但它仍然忽略了数组的第一次迭代。如果$i
设置为0
,则会向表中添加一条null
记录。
请参见下文,$rID_s 的值是在循环的第一次迭代中未分配的变量。提前致谢:
for ($i=1; $i <= $totalRows_rsClassReg; $i++) {
$row = mysql_fetch_array($rsClassReg);
$rID_s = $row['class_registry_student_ID_fk'];
mysql_select_db($database_SCOPE_test, $SCOPE_test);
$sql_aInstance = sprintf("INSERT INTO assignment_registry (assignment_reg_assignment_ID_fk, assignment_reg_student_ID_fk) VALUES (%s, %s)",
GetSQLValueString($aID, "int"),
GetSQLValueString($rID_s, "int"));
$Insert_aInstance = mysql_query($sql_aInstance, $SCOPE_test) or die(mysql_error());
echo $row['class_registry_student_ID_fk'] . " - ";
echo "Instanced";
echo "</br>";
}
I have a for loop that writes the results of a query into a table. I have a variable ($rID_s
) that is being assigned from a value in the query. For some reason, it omits the first iteration. I have a variable for the total rows of the query and it is assigning the correct number. I've tried setting $i = 0
and $i = 1
and it still omits the first iteration of the array. If $i
is set to 0
, it adds a null
record to the table.
See below, the value for $rID_s is the variable that is not being assigned on the first iteration of the loop. Thanks in advance:
for ($i=1; $i <= $totalRows_rsClassReg; $i++) {
$row = mysql_fetch_array($rsClassReg);
$rID_s = $row['class_registry_student_ID_fk'];
mysql_select_db($database_SCOPE_test, $SCOPE_test);
$sql_aInstance = sprintf("INSERT INTO assignment_registry (assignment_reg_assignment_ID_fk, assignment_reg_student_ID_fk) VALUES (%s, %s)",
GetSQLValueString($aID, "int"),
GetSQLValueString($rID_s, "int"));
$Insert_aInstance = mysql_query($sql_aInstance, $SCOPE_test) or die(mysql_error());
echo $row['class_registry_student_ID_fk'] . " - ";
echo "Instanced";
echo "</br>";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提供足够的信息来回答您的问题,因此我将完全离开您的领域并提供另一种方法来做到这一点,这应该使您的问题过时。
这里有很多事情你可以而且应该做不同的事情。想象一下,您对数据库的查询是一辆公共汽车,每个查询都是一次公共汽车行程。最好在巴士上装满更多数据,然后再乘坐多次巴士行程。
这是一个免费赠品 - 很久以前,我曾经使用过这些功能。现在,我使用自定义查询生成器和 PDO,为自己节省了大量工作。干得好...
You did not provide enough information to answer your question, so I'm going to totally left field you and provide another way of doing this that should make your question obsolete.
There are a lot of things you could and should be doing differently here. Imagine that your queries to your database are a bus and each query is a bus trip. It is better to fill up your bus with more data, then to take multiple bus trips.
Here's a freebie - A long time ago, I used to use these functions. Now a days, I use a custom query maker and PDO and save myself a lot of work. Here you go...