dqk77945 2014-11-26 20:21
浏览 63
已采纳

在PHP中调用(循环)MySQL行列表会产生意外结果

Two snippets of PHP code:

The first one works fine, giving values from different rows sequentially.

while ($row = mysql_fetch_assoc($rst)) {
...
$link1 = $row['audio1'];
    if ($link1) {
    $link1 = $link1;
    }
...
}

The second one outputs the value from the first row and applies it to every subsequent row.

while ($row = mysql_fetch_assoc($rst)) {
...
if ($row['audio1']) {
    $link1 = $row['audio1'];
    }
...
}

I.e. where the first code sample generates

Audio1.mp3
Audio2.mp3
Audio3.mp3

the second one would generate

Audio1.mp3
Audio1.mp3
Audio1.mp3

The question is why those two similar implementations differ in output.

Another test case with ...s removed (clean loop code) yields a different picture: the first example only outputs one line (the first one), the second example's output is the same which is repeating lines.

MySQL table that is being used has the following structure: One table, INT (id), CHAR, SMALLINT, TINYTEXT, TEXT, TEXT, TINYTEXT (audio1). All of the rows are fully populated except for the last one (audio1) which is mostly empty and has non-NULL values somewhere in the middle (if we sort by id field). Replacing the 'audio1' with another column name yields correct results (for that field) in both cases.

Another addition is that I do indeed place a SELECT statement initially in mysql_query that filters out rows. Depending on the number of rows being returned the picture for the second example may be slightly different, like 3 non-repeating values followed by a repetition of the third value until the end.

  • 写回答

1条回答 默认 最新

  • dongnaigu2052 2014-12-05 22:40
    关注

    I fixed it. Long story short - it was really obvious: between loop cycles $link1 variable didn't change in case if wasn't true. What I needed to do was to put $link1 = ""; or unset ($link); right after while and in that case if I ever encountered an empty value for $row['audio1'] it would stay truly empty instead of reusing the old value for every new row.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式