Php 如何将数组插入mysql数据库?
我得到了以下数组:Php 如何将数组插入mysql数据库?,php,mysql,arrays,database,Php,Mysql,Arrays,Database,我得到了以下数组: $dati = array( "data" => array( 'address_complete'=>$data->results[0]->formatted_address, 'address_square'=>$data->results[0]->address_components[1]->long_name, 'location'=>$data->r
$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
)
);
我想在数据库中插入$dati[“数据”][“位置]。
我该怎么修理
mysql_query("INSERT into utenti(city) VALUES ('$dati[data][location]')") or die (mysql_error());
首先,使用适当的变量插值:
mysql_query("INSERT into utenti(city) VALUES ('{$dati["data"]["location"]}')") or die (mysql_error());
其次,
mysql\uu
扩展已被弃用,请改用MySQLi
或PDO\umysql
PHP的解析器不是贪婪的,双引号字符串中的多维数组无法正确解析。e、 g
$arr['a']['b'] = 'c';
echo "$arr[a][b]";
将打印Array[b]
,而不是像您预期的那样打印c
,因为PHP在第一个[]
键之后停止对数组的解析
您必须使用{}
扩展符号:
echo "{$arr['a']['b']}"; // prints c as expected
使用下面这样的东西
mysql_query("INSERT into utenti(city) VALUES ('".$dati['data']['location']."')") or die (mysql_error());
注意:mysql_uuu扩展已被弃用,请改用MySQLi或PDO_u-mysql
将
$dati[data][location]
更改为$dati['data']['location']]
?当前发生了什么?开始时,请使用它:error@maximus2012警告:如果您只是在学习PHP,请不要使用该界面。它是如此可怕和危险,以至于在PHP7中被删除。替换类和指南类说明了最佳实践。您的用户参数不存在,并且存在可被利用的参数。有关SQL注入或不推荐使用的mysql_*扩展的任何评论?@devlentinate,请参阅我的评论使用{}
将数组括起来需要在数组键上使用引号,否则您将收到未定义的常量警告。@MarcB,当然,我已经错过了在深夜写这篇文章的机会。谢谢你的提示