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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动