Looping through a csv file to insert data to MySQL using php mysqli prepared statements -
ok i'll start code:
include('connect.php'); // prepared statement, prepare if (!($stmt = $mysqli->prepare("insert funddata(ticker, pricedate, price) values (?, ?, ?)"))) { echo "prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } //bind parameters query $csvdata = array("","",""); //initialise $csvdata array $pricedate = date($csvdata[1]); $stmt -> bind_param("ssd", $csvdata[0], $pricedate, $csvdata[2]); $file_handle = fopen($csvfile, "r"); while (!feof($file_handle) ) { $csvdata = fgetcsv($file_handle, 1024); $pricedate = date($csvdata[1]); echo $csvdata[0] . "; " . $pricedate . "; ". $csvdata[2] . "<br/>"; //run insert statement $stmt->execute(); } fclose($file_handle);
this fails insert data database table, although result in load of blank rows being inserted.
the csv data being correctly read evidenced echo
ing of inside loop. have gone wrong here?
Comments
Post a Comment