您有两种解决方案:
- You can either not use the first line of your
.dat
file
- 我想你正在逐行阅读......
- 我所以,只是不要使用第一行(您可以使用变量作为计数器来知道您是否在第一行)
- 或者,您可以在将数据插入数据库之前测试当前行是否仅包含整数/数字。
如果您选择第二个解决方案,以下是可以作为起点的代码部分:
if (in_numeric($data[0]) && is_numeric($data[1]) && is_numeric($data[2])) {
$sql_qry = "INSERT INTO DatTable (DF_PARTY_ID,DF_PARTY_CODE,DF_CONNECTION_ID)
VALUES ('$data[0]','$data[1]','$data[2]')";
$stmt = $this->connection->prepare($sql_qry);
$stmt->execute();
$this->checkForErrors($stmt);
}
另请注意,您正在使用prepare
and execute
,这似乎表明您正在尝试使用准备好的语句。
当使用准备好的语句时,你不应该像你正在做的那样做;你应该 :
- 一次且仅有一次:准备语句,使用数据占位符
- For each line, bind the values
你不应该 :
- 为每一行准备语句
- 将数据注入 SQL 查询中,而不是使用占位符。
这意味着你的代码应该看起来有点像这样(未经测试,因此您可能需要更改一些内容) :
// First, prepare the statement, using placeholders
$query = "INSERT INTO DatTable (DF_PARTY_ID,DF_PARTY_CODE,DF_CONNECTION_ID)
VALUES (:party_id, :party_code, :connection_id)";
$stmt = $this->connection->prepare($query);
if (in_numeric($data[0]) && is_numeric($data[1]) && is_numeric($data[2])) {
// Then, for each line : bind the parameters
$stmt->bindValue(':party_id', $data[0], PDO::PARAM_INT);
$stmt->bindValue(':party_code', $data[1], PDO::PARAM_INT);
$stmt->bindValue(':connection_id', $data[2], PDO::PARAM_INT);
// And execute the statement
$stmt->execute();
$this->checkForErrors($stmt);
}