这个
return statement
将结束您的方法的执行-并退出循环。引用手册:
如果从函数中调用,返回语句将立即
结束当前函数的执行,并将其参数返回为
函数调用的值。
为了让这件事顺利进行,我只会
return false
什么时候
$this->db->execute()
失败和
return true
在你的方法结束时,就像这样:
public function importBooks($data, $nr)
{
// Init query
$this->db->query('INSERT INTO books_pre (title, author, publication, container, created_by, created_at) VALUES (:title, :author, :publication, :container, :created_by, now())');
for ($i=2; $i<$nr; $i++) {
// Bind values
$this->db->bind(':title', $data[$i]['B']);
$this->db->bind(':author', $data[$i]['C']);
$this->db->bind(':publication', $data[$i]['D']);
$this->db->bind(':container', $data[$i]['E']);
$this->db->bind(':created_by', $_SESSION['user_id']);
// Execute query
if (!$this->db->execute()) {
return false;
}
}
return true;
}
但是,如果我不得不重写这段代码,我个人可能会将数据数组内爆并将其全部插入到一个查询中。