So I'm developing a web application in php that's grabbing a .csv file and parsing through it. It then submits it to a mysql database. I'm successfully retrieving the data and I'm able to input all of the numerical values that I'm retrieving. The one problem I'm having is with inserting the date into the sql database.
I'm not getting an error that I'm aware of and the date values are just 0000-00-00. I'm error checking by echoing the data in php to the screen when I run the script. It echoes fine and is in the format of yyyy-mm-dd. Below is php code that will run fine if you want to give it a test run, unfortunately I don't have a public server to let you test it on if you don't have wamp/lamp/equivalent already running on your system.
Am I missing something simple? Why won't this date insert correctly? Is it the wrong format? It looks to be the right format, but who knows?
<?php
//Connect to Database
$con=mysqli_connect("localhost","root","","stockmarket");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Setup URL to download csv file
$requestUrl = "http://ichart.yahoo.com/table.csv?s=GOOG";
// Pull data (download CSV as file)
$CSV = file_get_contents($requestUrl);
// Split results, trim way the extra line break at the end
$quotes = explode("\n",trim($CSV));
//Explore array with .csv contents
foreach($quotes as $quoteraw) {
$quoteraw = str_replace(", I", " I", $quoteraw);
$quote = explode(",", $quoteraw);
echo $quote[0];//error check, should print dates in correct format
//Insert contents into database
mysqli_query($con,"INSERT INTO nasdaq (Symbol,Date,Open,High,Low,Close,Volume,Adjusted Close) VALUES ('goog',$quote[0],$quote[1],$quote[2],$quote[3],$quote[4],$quote[5],$quote[6])");
}
mysqli_close($con);
?>
date
is a reserved keyword but is permitted to use without escaping it. The real problem is one of the columns contains space in between. To the OP, you need to escape it using single quote. – 今 草 顿 웃 Jul 8 at 0:16mysqli_error($con)
would be useful in this case -mysqli_query($con,"INSERT INTO nasdaq ... ) or die(mysqli_error($con));
– Sean Jul 8 at 0:32