Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在While循环中为每个迭代运行公共函数_Php_Mysql - Fatal编程技术网

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

Php 在While循环中为每个迭代运行公共函数,php,mysql,Php,Mysql,我是PHP新手,已经做了一些关于如何编写页面代码以运行代码隐藏的教程。我很难弄清楚为什么我的While循环只在循环的第一次迭代中执行一个公共函数。有什么想法吗 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); $info = array("name" => "", "class" => "", "description" => "", "characteristics"

我是PHP新手,已经做了一些关于如何编写页面代码以运行代码隐藏的教程。我很难弄清楚为什么我的While循环只在循环的第一次迭代中执行一个公共函数。有什么想法吗

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]);}
问题解决

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


查询失败,因为它从源电子表格中读取的内容需要转义

您应该在for循环中调用函数以获得结果
$[$c]=$data[$c]这似乎很奇怪。您还将关联数组分配给
$info
(即使用命名键),但在调用
create\u record
时尝试使用索引键。也无法确定这是否是整个
while
循环。创建一个最小的示例-删除代码,直到只有
while
循环和一个
变量转储($data)并从那里开始工作。编辑以澄清这是一个相当简单的循环,它显然在阵列正常打印时工作。最后一行是循环中唯一不随每次迭代一起运行的部分,循环的其余部分是上下文“最后一行是循环中唯一不随每次迭代一起运行的部分,”…可能查询由于某种原因失败。您是否已打开错误报告和/或日志记录?当查询失败时,数据库库是否抛出异常,或者是否需要手动检查(有时这是一个php配置问题)?
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 . "')");