douxi2670 2012-02-03 20:44
浏览 57
已采纳

PHP for循环变量未分配

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>";
    }
  • 写回答

1条回答 默认 最新

  • doutang2382 2012-02-03 21:01
    关注

    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.

    // do this up here
    mysql_select_db($database_SCOPE_test, $SCOPE_test);
    
    if( ! empty( $oneDimensionalArrayOfIds ) ){
        $query = " SELECT `col` FROM `table` WHERE ( ". implode( 'AND', $oneDimensionalArrayOfIds ) ." ) ";  // the 1-D array is getting your r_IDs or whatever
        $rsClassReg = sqlarr( $query );
        if( ! empty( $rsClassReg ) ){
            $sqlInserts = NULL;
            foreach( $rsClassReg as $r ){
                $sqlInserts[] =  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"));  
                // not sure where you are getting AIDs from (africa?  har har)...
            }
            if( ! empty( $sqlInserts ) ){
                sqlquery( implode( "; ", $sqlInserts ) );
            }
        }
    }
    

    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...

    function sqlarr($sql, $numass=MYSQL_BOTH) {
        // MYSQL_NUM  MYSQL_ASSOC  MYSQL_BOTH
        $got = array();
        $result=mysql_query($sql) or die("$sql: " . mysql_error());                             
    
        if(mysql_num_rows($result) == 0)
            return $got;
        mysql_data_seek($result, 0);
        while ($row = mysql_fetch_array($result, $numass)) {
            array_push($got, $row);
        }
        return $got;
    } 
    
    // Sql fetch assoc
    function sqlassoc($sql){
        $query = mysql_query($sql) or die("$sql:". mysql_error());
        $row = mysql_fetch_assoc($query);
        return $row;
    }
    
    function sqlrow($sql){
        $query = mysql_query($sql) or die("$sql:". mysql_error());
        $row = mysql_fetch_row($query);
        return $row;
    }
    
    function sqlquery($sql){
        $query = mysql_query($sql) or die("$sql:". mysql_error());
        return $row;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP