doufan1899 2011-07-17 07:02 采纳率: 100%
浏览 31
已采纳

基本的PHP / mySQL问题...遍历表中的每一行

I need to cycle through each row for the particular field, grabbing its content, run it through a parser, and place the result back in the field. I rarely have to delve this far into things, so I working example would be greatly appreciated. Here's where I am at so far (please don't laugh!):

<?

    function parse_string($text){
        // do something with $text...
        return $text
    }

    $username="abcd";
    $password="1234";
    $database="abcdefg";
    $table="table2"
    $field="field3"

    mysql_connect(localhost,$username,$password);

    @mysql_select_db($database) or die( "Unable to select database");

    // And for the part I'd like help with...
    // Repeat through each row of $table, parsing the content of $field 
    // via the function parse_string($text) and placing the result back 
    // into the database (replacing $field)


    mysql_close();

?>
  • 写回答

2条回答 默认 最新

  • doutuo3935 2011-07-17 07:14
    关注
    function parse_string($text){
    // do something with $text...
    return $text
    }
    
    $result = mysql_query("SELECT * FROM [ TABLE NAME ]");
    while(($row = mysql_fetch_assoc($result)){
        $myresult[] = $row;
    }
    
    array_walk( $myresult, 'parse_string' );
    

    I suggest use "CASE THEN" statement for your update query ( it's just 1 query instead of few like below )!

    UPDATE [ TABLE NAME ] SET [FIELD] = CASE `id` WHEN 1 THEN XXXXX ...... END, 
                              [FIELD] = CASE `id` WHEN 1 THEN XXXXX ...... END, 
                              .
                              .
                              .
                              .
                              WHERE ..........
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥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 图论 物流运输优化