简体   繁体   English

While循环中变量变化的值

[英]Value of variable changes in while-loop

Me and all my live contact are stunned by phenomenom where a variable value changes in other side ow while. 现象让我和我所有的现场联系人都震惊了,而另一边的变量值却在变化。 Before 2nd loop value is correct, but inside 2nd loop value is incorrect. 第2次循环之前的值正确,但第2次循环内部的值不正确。

Here's the actual code. 这是实际的代码。

try {
    $yhteys = new PDO('mysql:host=localhost;dbname=XXXX', 'YYYY', 'ZZZZ');
} catch (PDOException $e) {
    die("VIRHE: " . $e->getMessage());
}

$yhteys->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$yhteys->exec("SET NAMES utf8");

$kysely = $yhteys->prepare('SELECT viite FROM hakija WHERE vaihe = 1 ');
$kysely->execute();

$file = fopen("tilit.csv","r");
while(! feof($file)) {
    $tilirivi=fgetcsv($file,100,";");
    if ($tilirivi[4] < 0) continue;
    $viiteviesti = substr($tilirivi[3], 1);
//print "Viiteviesti1: $viiteviesti\n"; produces correct print
    while ($rivi = $kysely->fetch()) {
//print "Viiteviesti2: $viiteviesti\n"; produces incorrect print
        $kantaviite=$rivi["viite"];
        if ($viiteviesti == $kantaviite )  { 
            $asetus = $yhteys->prepare("UPDATE hakija SET vaihe=2 WHERE viite='$viiteviesti' "); 
            $asetus->execute();
        }
    }
}

How is this possible and how should I correct my code? 这怎么可能?我该如何更正我的代码?

'column' is a reserved variable name according to this article: 根据本文, 'column'是保留的变量名称:

http://hockinson.com/programmer-web-designer-denver-co-usa.php?s=43 http://hockinson.com/programmer-web-designer-denver-co-usa.php?s=43

which might cause unexpected results. 这可能会导致意外结果。

It came up that for some reason $kysely-fecth() didn't return any content. 由于某些原因$ kysely-fecth()没有返回任何内容。 I got the code working and here's solution: 我的代码正常工作,这是解决方案:

try {
    $yhteys = new PDO('mysql:host=localhost;dbname=XXXX', 'YYYY', 'ZZZZ');
} catch (PDOException $e) {
    die("VIRHE: " . $e->getMessage());
}

$yhteys->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$yhteys->exec("SET NAMES utf8");

$kysely = $yhteys->prepare('SELECT viite FROM hakija WHERE vaihe = 1 ');
$kysely->execute();
$kysely->setFetchMode(PDO::FETCH_NUM);
$result = $kysely->fetchAll();

$file = fopen("tilit.csv","r");
while(! feof($file)) {
    $tilirivi=fgetcsv($file,100,";");
    if ($tilirivi[4] < 10) continue;
    $viiteviesti = substr($tilirivi[3], 1);
    foreach ($result as $rivi) {
        foreach ($rivi as $kantaviite) {
            if ($viiteviesti == $kantaviite )  { 
                $asetus = $yhteys->prepare("UPDATE hakija SET vaihe=2 WHERE viite='$viiteviesti' "); 
                $asetus->execute();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM