Reputation: 117
It's not simple count of inserted rows into database. I'm running one query for each user, so it's not like I import 5 users at once with one query. It would be easy then and I would use affected_rows
or num_rows
. But with the code like this:
do {
if ($data[0]) {
//var_dump($data);
$query = "INSERT INTO `$tabela` (";
$last = end($anotherArray);
$numberOfValues=count($anotherArray);
if($numberOfValues==1){
$query.="$anotherArray[0])";
}else {
foreach ($anotherArray as $something) {
if ($something != $last) {
$query .= "" . $something . ",";
} else {
$query .= "" . $something . ")";
}
}
}
$query .= "VALUES (";
$lengtara = count($anotherArray);
$length1 = count($data);
foreach ($data as $d) {
$query .= "'$d',";
}
$query = rtrim($query, ',');
$query .= ")";
$rez = $mysqli->query($query);
$users_cnt=$mysqli->affected_rows;
}
} while ($data = fgetcsv($handle, 1000, ",", "'"));
when I run var_dump($users_cnt)
and lets say I inserted 3 new users, it shows NULL
, NULL
, NULL
. But it's not some array that I could take count
of. As you can see I'm, building query because it's structure depends on some dynamic data, and this is only way that works when it comes to inserting users. Please help if you have some suggestions.
Upvotes: 2
Views: 84
Reputation: 403
I don't see why you couldn't do this:
$count = 0
do {
if ($data[0]) {
//var_dump($data);
$query = "INSERT INTO `$tabela` (";
$last = end($anotherArray);
$numberOfValues=count($anotherArray);
if($numberOfValues==1){
$query.="$anotherArray[0])";
}else {
foreach ($anotherArray as $something) {
if ($something != $last) {
$query .= "" . $something . ",";
} else {
$query .= "" . $something . ")";
}
}
}
$query .= "VALUES (";
$lengtara = count($anotherArray);
$length1 = count($data);
foreach ($data as $d) {
$query .= "'$d',";
}
$query = rtrim($query, ',');
$query .= ")";
$rez = $mysqli->query($query);
$count++
$users_cnt=$mysqli->affected_rows;
}
} while ($data = fgetcsv($handle, 1000, ",", "'"));
Then just output $count
wherever you need it.
Upvotes: 1