PDO bindParam 使用数组,不按假设工作
PDO bindParam using an array, not working as assumed
我有以下代码...
$statement = $conn->prepare("insert into logtest (course, date, time, distance,
actualstarttime, finishtime, fav, favtotalmatched, favwma, 2ndfav,
2ndfavtotalmatched, 2ndfavwma, 3rdfav, 3rdfavtotalmatched, 3rdfavwma,
orangeflag, greenflag, betplaced, totalracetime, numcalculations,
secswaitedbeforemonitoring, monitorstarttime, totalmonitoringtime)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);");
for($i = 0; $i < 23; $i++){
$statement->bindParam($i + 1, $record[$i]);
}
$recordsprocessed = 0;
$line = fgets($file); // Do this once before the loop to go past
// the column headers line
while (!feof($file)){
$line = fgets($file);
$record = explode(",", $line);
print_r($record);
$statement->execute();
++$recordsprocessed;
}
echo "<p>Import finished. $recordsprocessed records imported.</p>";
代码正在用空值填充数据库。我不明白为什么。我使用 print_r
来验证 $record
数组确实包含值。
从页面 http://php.net/manual/en/pdo.prepared-statements.php 暗示参数可以在变量包含值之前绑定到变量,所以我假设我可以绑定到 $record
的项目然后填充这些值稍后循环。
我做错了什么?还是 bindParam 根本不适用于数组?
作为替代方案,您可以每行获取这些值,然后通过 ->execute()
添加这些批次
$query = 'INSERT INTO logtest
(course,date,time,distance,actualstarttime,finishtime,fav,favtotalmatched,favwma,2ndfav,2ndfavtotalmatched,2ndfavwma,3rdfav,3rdfavtotalmatched,3rdfavwma,
orangeflag,greenflag,betplaced,totalracetime,numcalculations,secswaitedbeforemonitoring,monitorstarttime,totalmonitoringtime)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
';
$statement = $conn->prepare($query);
$recordsprocessed = 0;
$line = fgets($file); // do this once before the loop to go past the column headers line
while (!feof($file)){
$line = fgets($file);
$record = explode(",",$line);
$statement->execute($record);
++$recordsprocessed;
}
echo "<p>Import finished. $recordsprocessed records imported.</p>";
我有以下代码...
$statement = $conn->prepare("insert into logtest (course, date, time, distance,
actualstarttime, finishtime, fav, favtotalmatched, favwma, 2ndfav,
2ndfavtotalmatched, 2ndfavwma, 3rdfav, 3rdfavtotalmatched, 3rdfavwma,
orangeflag, greenflag, betplaced, totalracetime, numcalculations,
secswaitedbeforemonitoring, monitorstarttime, totalmonitoringtime)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);");
for($i = 0; $i < 23; $i++){
$statement->bindParam($i + 1, $record[$i]);
}
$recordsprocessed = 0;
$line = fgets($file); // Do this once before the loop to go past
// the column headers line
while (!feof($file)){
$line = fgets($file);
$record = explode(",", $line);
print_r($record);
$statement->execute();
++$recordsprocessed;
}
echo "<p>Import finished. $recordsprocessed records imported.</p>";
代码正在用空值填充数据库。我不明白为什么。我使用 print_r
来验证 $record
数组确实包含值。
从页面 http://php.net/manual/en/pdo.prepared-statements.php 暗示参数可以在变量包含值之前绑定到变量,所以我假设我可以绑定到 $record
的项目然后填充这些值稍后循环。
我做错了什么?还是 bindParam 根本不适用于数组?
作为替代方案,您可以每行获取这些值,然后通过 ->execute()
$query = 'INSERT INTO logtest
(course,date,time,distance,actualstarttime,finishtime,fav,favtotalmatched,favwma,2ndfav,2ndfavtotalmatched,2ndfavwma,3rdfav,3rdfavtotalmatched,3rdfavwma,
orangeflag,greenflag,betplaced,totalracetime,numcalculations,secswaitedbeforemonitoring,monitorstarttime,totalmonitoringtime)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
';
$statement = $conn->prepare($query);
$recordsprocessed = 0;
$line = fgets($file); // do this once before the loop to go past the column headers line
while (!feof($file)){
$line = fgets($file);
$record = explode(",",$line);
$statement->execute($record);
++$recordsprocessed;
}
echo "<p>Import finished. $recordsprocessed records imported.</p>";