Reputation: 4187
I Get NULL when i insert the Birthday and current time in MYSQL
$graph_url = "https://graph.facebook.com/me?access_token=".$params['access_token']."&format=json&fields=birthday";
$user = json_decode(file_get_contents($graph_url),true);
echo("birthday is " . $user['birthday']);
SQL
$queryip = "insert into table (birthday,currenttime) values (".$user['birthday'].','.CURDATE().")";
$resultip = mysql_query($queryip);
MYSQL Structure
Both the birthday,currenttime fields are Type DATE
Upvotes: 1
Views: 1062
Reputation: 2767
Try
$user['birthday'] = date("Y-m-d", strtotime($user['birthday']));
before the SQL insert statement.
Upvotes: 0
Reputation: 7664
In PHP:
function formatDateFbToSql($fbdate)
{
// facebook user_birthday format (02/19/1988)
$date = DateTime::createFromFormat('m/d/Y', $fbdate);
// mysql format (1988-02-19)
return $date->format('Y-m-d');
}
Upvotes: 2
Reputation: 122679
(Despite being a different language and RDBMS, this is the same problem as in this question.)
Firstly, SQL strings must be contained in ''
: you're clearly missing them in (".$user['birthday'].' ...
. Secondly, and more importantly, they must be escaped, to prevent SQL injection attacks (and to make sure the string is valid anyway).
In general, it's better to use prepared statements to pass the parameters to SQL query instead of inserting the variables in the query itself: this should take care of the escaping and conversion for you.
You'll also need to cast the birthday string into a date if necessary.
I'd recommend using mysqli
and prepared statements instead. (There are a number of examples out there, perhaps this would help.)
EDIT:
Try something along these lines perhaps (assuming you've switched to mysqli
):
$query = "insert into table (birthday,currenttime) values (STR_TO_DATE(?, '%m/%d/%Y'), CURDATE())";
if ($stmt = $mysqli->prepare()) {
$stmt->bind_param('s', $user['birthday']);
$stmt->execute();
$stmt->close();
} else {
die($mysqli->error);
}
(This might need to be birthday_date
as Jeroen suggested too.)
Upvotes: 1
Reputation: 13257
You need to request the birthday_date
column, not birthday
, because it's always in the same format, DDMMYYY.
After that, you need to add the following to your INSERT query:
STR_TO_DATE('" . mysql_real_escape_string ($user ['birthday_date']) . "', '%m/%d/%Y')
Upvotes: 2
Reputation: 716
Did you try replacing ".$user['birthday'].','.CURDATE()." into '".$user['birthday']."',CURDATE() ?
Upvotes: 0