I need to make a simple CSV->MySQL script in OOP and I was wondering if there's anything I am doing wrong in a object-oriented sense.
I have a file name db.php where I connect to MySQL via MySQLi and then I have another class where I load a csv file into MySQL like this:
class csv extends Database {
public $file = null;
function __construct() {
if(count($_SERVER["argv"]) > 1){
$this->file = $_SERVER["argv"][1];
}else{
$this->file = "stock.csv";
}
$this->db = $db;
}
function saveCSVtoDB(){
//opens the file
$fp = fopen($this->file, "r");
$db = Database::getInstance();
$mysqli = $db->getConnection();
$result = $mysqli->query("LOAD DATA LOCAL INFILE 'file.csv'
IGNORE INTO TABLE products
CHARACTER SET UTF8
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(code, name, desc, stock, price, @avai)
SET available = IF(@avai LIKE '%yes%', 1, 0)
");
if($result){
echo "success";
}else{
die("error");
}
}
}