I've got a hard one on hand: mysqli can't find [some] existing records. Any help would be appreciated.
Case in point: I truncate the table (empty it);
On a first run, I fill it up with 2,495 records from a text file. Just to make sure things are correct (since I verified inconsistencies), I re-run the very same file input. 12 (always the same 12) records can't be located and are re-inserted instead of updated. I checked the file and re-entered them by hand on the text version.
The error persists: on the second run, there are 2,508 records and, of course, 12 duplicates.
Code:
$result = $DB->query("SELECT * FROM `inventory-temp` WHERE MFR='".$MFR."' AND SKU='".$SKU."'");
$upd = false;
if ($result->num_rows > 0) {
if (($result->row['MFR'] == $MFR) && ($result->row['SKU'] == $SKU)) {
$upd = true;
}
unset($result);
}
if ($upd) {
$DB->query($update);
echo $i, ' = Record updated! <br />';
} else {
$DB->query($insert);
echo $i, ' = New record created! <br />';
}
I believed the problem was the num_rows
return; so I re-checked within the if statement.
The problem continued, so I added the unset
trying to see if it had anything to do with crumbs.
I also tried ->real_query
. No changes. What could I, possibly, be missing?
UPDATE: this is really killing me!
On the 'invisible' character(s), it was a no go. I replaced the original records by hand (on the xls file); re-exported to CSV; and re-tried a few times. Still the same problem.
I failed to inform that I have an UPDATED
field with CHAR
of lenght of 1
character. Before I do the updates in bulk, I clear that FLAG
on all records and update them with *
as the record is inserted
or updated
.
I changed the check code to include that:
$result = $DB->query("SELECT * FROM `inventory-temp` WHERE MFR='".$MFR."' AND SKU='".$SKU."' AND UPDATED=' '");
It still fails to find the existing
records.
What is even funnier (not really) is that when I check the table, visually, both the original and duplicate records DO have the UPDATED field with *.
Now is the kicker:
As the records are inserted or updated, I echo the record number (sequence number) and the MFR
just for some visual aid.
On the second run, I get the 12 (always the same) Record inserted
instead of Record updated
.
When I go back to the XLS file for the corresponding line number
, the record inserted IS NOT THE ONE listed; but a similar one for a different MFR
with the same SKU
.
Mind you that THIS
MFR
has a lenght of 4 characters. The duplicated ones have the length of 11 characters.
Very puzzling ....
These fields had, respectively, CHAR 15 and CHAR 20. For test I changed them to both to TinyText. The problem still happens. Very weird ....
UPDATE (2)
: I went ahead and, instead of a csv input, I made a plain text file delimited by TAB
; a.k.a. chr(9)
. Re-ran everything multiple times.
===> The problem persists.
"When all fail, the obvious .."
Okay, I will, for test, remove ALL records for that MFR
and re-run everything.
UPDATE 3:
Okay, trying to solve the puzzle, I went ahead and changed the length of those two columns (every listing) by adding or removing characters like aaaa
to make
them up to the column width (15 chars).
Nothing worked. I did that for both of the culprits MFR
1 and 2.
No solution.
The idea was to be able to update the inventory file in real time instead of after hours by updating just the modified records instead of the entire table when dumping the CSV file replacing the table contents.
Oh well, for now it will be after hours. If I run into a solution, I will posted it here.
I just wonder what else the 'i' in MySQL have changed.
Thank you for your trying ...