簡體   English   中英

在While循環中為每次迭代運行公共功能

[英]Running public function for each iteration in a While Loop

我是PHP新手,並且完成了一些有關如何對頁面進行編碼以在后面運行代碼的教程。 我在弄清楚為什么While循環僅對循環的第一次迭代執行Public Function時遇到了麻煩。 有任何想法嗎?

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$info = array("name" => "", "class" => "", "description" => "", "characteristics" => "");
        for  ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
            $info[$c] = $data[$c];
        }
        echo print_r($info);
        infoDB::getInstance()->create_record($info[0],$info[1],$info[2],$info[3]);}

上面的代碼為While循環的每次迭代打印$ info數組,但是create_record函數僅將第一次迭代的結果插入到相應的MYSQL數據庫中。 PHP / Instantiation中的邏輯是否有固有的含義,意味着它僅在第一次迭代時執行?

以下是其供參考的函數(在第一次迭代中正常工作,然后不再出現)

    public function create_record ($info, $class, $description, $characteristics) {
    $this->query("INSERT INTO tbl_info (toon, zeta, description, characteristics)" . " VALUES ('" . $toon . "', '" . $zeta . "', '" . $description . "', '" . $characteristics . "')");
}

問題解決了

public function create_record ($info, $class, $description, $characteristics) {
$this->query("INSERT INTO tbl_info (toon, zeta, description, characteristics)" . " VALUES ('" . $toon . "', '" . $zeta . "', '" . $description . "', '" . $characteristics . "')");

}

就是現在

public function create_record ($info, $class, $description, $characteristics) {
    $name = $this->real_escape_string($name);
    $class = $this->real_escape_string($class);
    $description = $this->real_escape_string($description);
    $characteristics = $this->real_escape_string($characteristics);
$this->query("INSERT INTO tbl_info (toon, zeta, description, characteristics)" . " VALUES ('" . $toon . "', '" . $zeta . "', '" . $description . "', '" . $characteristics . "')");

}

由於要從源電子表格中讀取的內容需要轉義,因此查詢失敗

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM