[英]How to insert an array into mysql database?
I've got the following array: 我有以下数组:
$dati = array(
"data" => array(
'address_complete'=>$data->results[0]->formatted_address,
'address_square'=>$data->results[0]->address_components[1]->long_name,
'location'=>$data->results[0]->address_components[2]->long_name,
'postal_code'=>$data->results[0]->address_components[7]->long_name,
'data_ora'=>$tmp_date
)
);
and I want to insert $dati["data"]['location'] in database.
我想
$dati["data"]['location'] in database.
插入$dati["data"]['location'] in database.
How can I fix 我该如何解决
mysql_query("INSERT into utenti(city) VALUES ('$dati[data][location]')") or die (mysql_error());
? ?
At first , use proper variable interpolation: 首先 ,使用适当的变量插值:
mysql_query("INSERT into utenti(city) VALUES ('{$dati["data"]["location"]}')") or die (mysql_error());
Secondly , mysql_
extension is deprecated, use MySQLi
or PDO_MySQL
instead. 其次 ,不建议使用
mysql_
扩展名,而是使用MySQLi
或PDO_MySQL
。
PHP's parser is not greedy, and multi-dimensional arrays in double-quoted strings will NOT parse properly. PHP的解析器不是贪婪的,并且双引号字符串中的多维数组将无法正确解析。 eg
例如
$arr['a']['b'] = 'c';
echo "$arr[a][b]";
will print Array[b]
, not c
as you expect, because PHP stops parsing for arrays after the first []
key. 将打印
Array[b]
,而不是您期望的c
,因为PHP在第一个[]
键之后停止解析数组。
You have to use the {}
extended notation: 您必须使用
{}
扩展符号:
echo "{$arr['a']['b']}"; // prints c as expected
Use somthing like below 像下面这样使用
mysql_query("INSERT into utenti(city) VALUES ('".$dati['data']['location']."')") or die (mysql_error());
Note: mysql_ extension is deprecated, use MySQLi or PDO_MySQL instead.
注意:不建议使用mysql_扩展名,请改用MySQLi或PDO_MySQL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.