lcook wrote:1. I know I need to change 2141 to 2143 in my while arguments
Personally, I disagree. I would instead use some larger value (e.g. 4096) there just to make sure you're reading in the entire line. The way you have it, if a line exceeds 2142 bytes then your code wouldn't be able to tell that it read in a malformed line and would instead parse it as if it were the correct length. To me, this is bad.
lcook wrote:2. I did have $db->error; within my while statement
Yes, but you weren't a) checking to see if the query failed, b) using that error value if the query did indeed fail, and c) doing anything useful with the error value (e.g. outputting and/or logging it in a manner such that you can easily pair it with either the data in the file or the full SQL query that was executed which caused the error in the first place).
lcook wrote:how do I make it line-specific in the code above?
You'll have to keep track of line numbers yourself - [man]fgets/man and the like don't tell you this (they can only tell you what "position" the pointer is at in the file, which doesn't necessarily give you the line number - e.g. if a malformed line was indeed encountered which would throw off the expected byte count).
To do so yourself, simply initialize a counter variable ($line_num is descriptive, though $i is often used for such mundane practices as counting things) before the while loop and then increment it at the end of each loop iteration.
Also note that instead of this:
$i = 1;
while()
{
// ..
$i++;
}
you could be fancy and switch to a for() loop instead, e.g.:
for($i = 0; ($buffer = fgets($handle, 2143)) !== false; $i++)
{
// ...
}
lcook wrote:I know how to put 'ignore 1 line' into the LOAD DATA LOCAL INFILE query, but how do you do it with fgets()?
I can immediately think of two ways to go about doing this:
Inside the looping structure, check to see if the line number you're on is less than or equal to the number of lines you wish to skip. If it is, simply [man]continue[/man] the loop rather than doing any processing.
Before the loop structure, simply call [man]fgets/man once for every line you wish to skip. Since you're skipping these lines, you don't need to store the return value anywhere.