-1

我在 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);

?>

问候

4

1 回答 1

0

好吧,根据用途,您可以将其存储为 json 字符串(以防您想将其直接发送到浏览器并在那里使用它)或创建一个表,其中所有数组值作为列。然后做一个插入。

为了确保插入或更新安全,您在这里有一个很好的例子,使用准备好的语句和 PDO。

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

于 2012-11-03T18:14:52.623 回答