将关联数组数据插入mysql

时间:2012-11-03 18:01:35

标签: mysql

我在php中以关联数组格式输出以下twitter数据,如何将此数据插入到mysql数据库表中。请建议采取任何方式。

Array (
 [created_at] => Fri, 02 Nov 2012 18:15:57 +0000 
[from_user] => VolksblattNews 
[from_user_id] => 828753642 
[from_user_id_str] => 828753642 
[from_user_name] => Volksblatt News 
[geo] => [id] => 2.6443066310356E+17 
[id_str] => 264430663103557632 
[iso_language_code] => de 
[metadata] => Array ( [result_type] => recent ) 
[profile_image_url] => http://a0.twimg.com/profile_images/2619216207/VB_48x48_reasonably_small_normal.gif 
[profile_image_url_https] => https://si0.twimg.com/profile_images/2619216207/VB_48x48_reasonably_small_normal.gif 
[source] => <a Google</a> => Bundesrat Burkhalter verstärkt Schweizer Beziehungen zu Burma: RANGUN - Didier Burkhalter hat… http://t.co/BF3aX2bY 
[to_user] => 
[to_user_id] => 0 
[to_user_id_str] => 0 [
to_user_name] => )

我从以下程序获得此输出:

 <?php

//a json string

$jsonstring='{


"created_at": "Fri, 02 Nov 2012 18:15:57 +0000", 
   "from_user": "VolksblattNews", 
   "from_user_id": 828753642, 
   "from_user_id_str": "828753642", 
   "from_user_name": "Volksblatt News", 
   "geo": null, 
   "id": 264430663103557632, 
   "id_str": "264430663103557632", 
   "iso_language_code": "de", 
   "metadata": {
    "result_type": "recent"
   }, 
   "profile_image_url": "http://a0.twimg.com/profile_images/2619216207/VB_48x48_reasonably_small_normal.gif", 
   "profile_image_url_https": "https://si0.twimg.com/profile_images/2619216207/VB_48x48_reasonably_small_normal.gif", 
   "source": "&lt;a href=&quot;http://www.google.com/&quot;&gt;Google&lt;/a&gt;", 
   "text": "Bundesrat Burkhalter verst\u00e4rkt Schweizer Beziehungen zu Burma: RANGUN - Didier Burkhalter hat\u2026 http://t.co/BF3aX2bY", 
   "to_user": null, 
   "to_user_id": 0, 
   "to_user_id_str": "0", 
   "to_user_name": null

}';

$arrayvariable=json_decode($jsonstring,true);

print_r ($arrayvariable);

?>

此致

1 个答案:

答案 0 :(得分:0)

根据用途的不同,您可以将其存储为json字符串(如果您想将其直接发送到浏览器并在其中使用)或创建一个包含所有数组值的表作为列。然后插入。

为了使插入或更新安全,你可以在这里使用预处理语句和PDO。

http://www.phpeveryday.com/articles/PDO-Insert-and-Update-Statement-Use-Prepared-Statement-P552.html