Php 使用var_dump()时变量different的值
我有一个将CSV文件插入数据库的功能。我的项目使用Yii框架,但这个类与Yii无关 导入成功,但在打印导入的行数时遇到问题 我的类有多个文件,但只有一个文件:Php 使用var_dump()时变量different的值,php,csv,import,var-dump,Php,Csv,Import,Var Dump,我有一个将CSV文件插入数据库的功能。我的项目使用Yii框架,但这个类与Yii无关 导入成功,但在打印导入的行数时遇到问题 我的类有多个文件,但只有一个文件: class LsuController extends BackEndController { public function dbconfig(){ $dbconfig = array(); $dbconfig['dsn'] = 'mysql:host=localhost;dbname=bs_de
class LsuController extends BackEndController {
public function dbconfig(){
$dbconfig = array();
$dbconfig['dsn'] = 'mysql:host=localhost;dbname=bs_dev_csueastbay';
$dbconfig['username'] = 'root';
$dbconfig['password'] = '';
return $db = new PDO($dbconfig['dsn'], $dbconfig['username'], $dbconfig['password']);
}
public function actionIndex() {
// Config database
$db = $this->dbconfig();
$has_err = 0;
$link = dirname(__FILE__)."/../../../runtime/Import/";
$handles = array();
$handles[] = fopen($link."bookPictureMappings.csv","r");
foreach ($handles as $handle) {
if(!$handle){
$has_err++;
}
}
if($has_err == 0){
foreach ($handles as $key=>$handle) {
switch ($key) {
case '0':
$i = 0;
$model_err = 0; $ss = 0;
do {
if (isset($data[0])) {
if($i > 0){
$sql="INSERT INTO bookpicturemappings (Id, BookId, PictureId, bookpicturemappings.Order) VALUES (
:Id, :BookId, :PictureId, :bookOrder)";
$command = $db->prepare($sql);
$command->bindParam(":Id",$data[0],PDO::PARAM_STR);
$command->bindParam(":BookId",$data[1],PDO::PARAM_STR);
$command->bindParam(":PictureId",$data[2],PDO::PARAM_STR);
$command->bindParam(":bookOrder",$data[3],PDO::PARAM_STR);
if ($command->execute()){
$ss++;
}
}
$i++;
}
} while ($data = fgetcsv($handle,0,",","\""));
echo "Insert bookpicturemappings.csv successful! Total: ".($i-1)." - Import: ".$ss." - Error: ".$model_err."<br>";
break;
...............................
结果是$ss=1862:
插入bookpicturemappings.csv成功!总数:1862-进口:1862
-错误:0
WT。。。?我不明白为什么?检查$handles中的最新项目,我猜它是文件末尾的空换行符?$ss设置为0,但不循环 编辑 您的上一次迭代为false,但仅在执行块后检查条件 尝试放弃do并像这样使用while:
while ($data = fgetcsv($handle,0,",","\"")){
if (isset($data[0])) {
if($i > 0){
$sql="INSERT INTO bookpicturemappings (Id, BookId, PictureId, bookpicturemappings.Order) VALUES ( :Id, :BookId, :PictureId, :bookOrder)";
$command = $db->prepare($sql);
$command->bindParam(":Id",$data[0],PDO::PARAM_STR);
$command->bindParam(":BookId",$data[1],PDO::PARAM_STR);
$command->bindParam(":PictureId",$data[2],PDO::PARAM_STR);
$command->bindParam(":bookOrder",$data[3],PDO::PARAM_STR);
if ($command->execute()){
$ss++;
}
}
$i++;
}
};
最新的项不是空的,我在一个项中回显$ss。您的最终$handle得到执行了吗?检查它说:除了真值表达式在每次迭代结束时而不是在开始时被检查之外。试一下不要“做”。还有一个潜在的缺陷:在第一次迭代中,$i等于零。这意味着您的第一条语句将不会被执行。当然,除非第一行应该被忽略。尝试添加ERRMODE_异常并向我们显示结果,可能您的PDO查询运行不正确。
while ($data = fgetcsv($handle,0,",","\"")){
if (isset($data[0])) {
if($i > 0){
$sql="INSERT INTO bookpicturemappings (Id, BookId, PictureId, bookpicturemappings.Order) VALUES ( :Id, :BookId, :PictureId, :bookOrder)";
$command = $db->prepare($sql);
$command->bindParam(":Id",$data[0],PDO::PARAM_STR);
$command->bindParam(":BookId",$data[1],PDO::PARAM_STR);
$command->bindParam(":PictureId",$data[2],PDO::PARAM_STR);
$command->bindParam(":bookOrder",$data[3],PDO::PARAM_STR);
if ($command->execute()){
$ss++;
}
}
$i++;
}
};