dsgdfh302506
2018-03-28 18:19
浏览 53
已采纳

在While循环中为每次迭代运行公共函数

I am new to PHP and have done a few tutorials on how to code pages to run code behind. I am having trouble though figuring out why my While loop is only executing a Public Function for the first iteration through the loop. Any ideas?

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 />
";
            $info[$c] = $data[$c];
        }
        echo print_r($info);
        infoDB::getInstance()->create_record($info[0],$info[1],$info[2],$info[3]);}

The code above prints the $info array for each iteration of the While loop, but the create_record function only inserts the results of the first iteration into the corresponding MYSQL Database. Is there something inherent to the logic in PHP/Instantiation that means it only executes on the first iteration?

Below is the function it is calling to for reference (works correctly for the 1st iteration and then does not recur)

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

图片转代码服务由CSDN问答提供 功能建议

我是PHP的新手,并完成了一些关于如何编写页面以运行代码的教程。 虽然我弄清楚为什么我的While循环只是在循环的第一次迭代中执行公共函数,但我遇到了麻烦。 有什么想法吗?

  while(($ data = fgetcsv($ handle,1000,“,”))!== FALSE){
 $ num = count($ data)  ); 
 $ info = array(“name”=&gt;“”,“class”=&gt;“”,“description”=&gt;“”,“characteristics”=&gt;“”); 
 for(  $ c = 0; $ c&lt; $ num; $ c ++){
 echo $ data [$ c]。  “&lt; br /&gt; 
”; 
 $ info [$ c] = $ data [$ c]; 
} 
 echo print_r($ info); 
 infoDB :: getInstance() - &gt;  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-&gt; query(“INSERT INTO tbl_info( 香椿,泽塔,描述,特征)“。”VALUES('“。$ toon。”','“。$ zeta。”','“。$ description。”','“。$ features。”')“  ); 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqd3690 2018-03-29 12:38
    已采纳

    Problem Solved

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

    }

    Is Now

    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 . "')");
    

    }

    The query was failing based on something it was reading in from the source spreadsheet needing to be escaped

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题