dougou8573 2013-05-19 13:47
浏览 12

too long

this below simple code i want to replace [3] to 2010-01-01 and [6] to edited in array fetch. i find function to edit in place with key but thats dont work correctly PHP:

while ( $aRow = mysql_fetch_array( $rResult ) )
{
    replace_key('id', '00', $aRow);
    $output['aaData'][] = $aRow;
}

RESULT:

Array
(
    [0] =>aaaaaaa
    [title] => bbbbbb
    [1] => 86
    [id] => 86
    [2] => rewr
    [subject] => rewr
    [3] => 0000-00-00
    [date_time] => 0000-00-00
    [4] => admin
    [username] => admin
    [5] =>cccc
    [6] =>ddddd
)
  • 写回答

1条回答 默认 最新

  • dongqin1819 2014-02-13 15:18
    关注

    Using mysql_fetch_array()'s default arguments, your SQL query returns both an indexed and associative array entry for each row.

    Firstly, I strongly suggest that you stay away from mysql_* style functions. This set of tools has been deprecated for a while now, and is no longer being updated. There's plenty of resources online which explain why in more detail, and some good alternatives. Alas, let's move on.

    Next, I advise that (if you need to use this function) you use one return format method or the other, by passing either MYSQL_ASSOC (associative array) or MYSQL_NUM (numbered array) into the function as it's second argument.

    E.g. MYSQL_ASSOC will give you:

    Array
    (
        [title] => bbbbbb
        [id] => 86
        [subject] => rewr
        [date_time] => 0000-00-00
        [username] => admin
    )
    

    Generally, this is favored above a numbered array because you don't have to rely on the order that you have selected your columns in within your query.

    E.g. MYSQL_NUM will give you:

    Array (
        [0] =>aaaaaaa
        [1] => 86
        [2] => rewr
        [3] => 0000-00-00
        [4] => admin
    )
    

    Right. Now, to edit the array in your while loop, you can just add the following lines:

    // Using MYSQL_ASSOC
    
    while ( $aRow = mysql_fetch_array( $rResult , MYSQL_ASSOC ) ) {
    
        $aRow['date_time'] = '2010-01-01';
        $aRow['status'] = 'edited'; // Whatever key this column should be needs to be added instead of 'status'
    
        $output['aaData'][] = $aRow;
    
    }
    

    Or:

    // Using MYSQL_NUM
    
    while ( $aRow = mysql_fetch_array( $rResult , MYSQL_NUM ) ) {
    
        $aRow[3] = '2010-01-01';
        $aRow[6] = 'edited';
    
        $output['aaData'][] = $aRow;
    
    }
    

    If you still want to stick with both, you'll need to use the lines from both examples:

    // Using DEFAULT / MYSQL_BOTH
    
    while ( $aRow = mysql_fetch_array( $rResult ) ) {
    
        $aRow['date_time'] = '2010-01-01';
        $aRow[3] = '2010-01-01';
    
        $aRow['status'] = 'edited'; // Whatever key this column should be needs to be added instead of 'status'
        $aRow[6] = 'edited';
    
        $output['aaData'][] = $aRow;
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号