需要SQL帮助来存储Twitter信息

时间:2011-02-10 11:36:24

标签: php sql ajax

我是开发新手

这是一个获取twitter配置文件的小代码,现在任何人都可以告诉我如何在sql数据库中存储这些数据。 thxs

<head>
<title>twitter example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function()
{
$('#btn').click(function()



{
    twitterusername = $('#uname').val();
    if(twitterusername != '')
    {
        $('#loading').text('Please wait...');
        $.getJSON('http://twitter.com/users/'+twitterusername+'.json?callback=?',
        function(data)
        {
            $('#loading').empty();
            $('#twit_uname').html('<b>'+data.screen_name+'</b>');
            $('#twit_frnd').html('<b>'+data.friends_count+'</b>');
            $('#twit_name').html('<b>'+data.name+'</b>');
            $('#twit_flwr').html('<b>'+data.followers_count+'</b>');
            $('#twit_img').html('<img src="'+data.profile_image_url+'" height="50" width="50">');
        });
    }
});
})
</script>
<style>
div{float:right; border-bottom:1px dashed #909; margin-bottom:2px;}
li{border-bottom:1px dashed #909; margin-bottom:10px;}
.main{margin:auto;border:1px solid #CC0;float:none;width:1000px;padding:10px;font-family:Verdana, Geneva, sans-serif;font-size:16px;color:#000000;}
b{float:left; text-align:left;}
</style>
</head>
</style>

<body>
<?php include "../header.php"; ?>
<div class="main">
Enter your Twitter Username : <input type="text" name="user" id="uname">
<input type="button" value="Get my Profile" id="btn"><br />
<div id="loading" align="center"></div>
<ul>
<li>Name : <div id="twit_name"></div></li>
<li>Username : <div id="twit_uname"></div></li>
<li>Followers : <div id="twit_flwr"></div></li>
<li>Following : <div id="twit_frnd"></div></li>
<li>Profile Image : <div id="twit_img" style="border:1px solid #999999; height:50; width:50;"></div></li>
<input type="button" value="Save to Database" id="btn1">
</ul>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

更新:插入db

时添加mysql_real_escape_string()

<强> JQUERY

$.getJSON('http://twitter.com/users/' + twitterusername + '.json?callback=?',
function(data) {
var json_data = data;
    $.ajax({
        type: "POST",
        url: "/post-twitter-json.php",
        data: "json=json&json_data=" + json_data,
        success: function(msg) {
            //alert( "Data Saved: " + msg );
            }
    });
    $('#loading').empty();
    $('#twit_uname').html('<b>' + data.screen_name + '</b>');
    $('#twit_frnd').html('<b>' + data.friends_count + '</b>');
    $('#twit_name').html('<b>' + data.name + '</b>');
    $('#twit_flwr').html('<b>' + data.followers_count + '</b>');
    $('#twit_img').html('<img src="' + data.profile_image_url + '" height="50" width="50">');
});

<强> MYSQL

CREATE TABLE twitter_json(
      tj_id         INT(11) NOT NULL AUTO_INCREMENT,
      tj_user_id    INT(11) NOT NULL DEFAULT 0,
      tj_json       TEXT NOT NULL
      PRIMARY KEY (tj_id)
   ) TYPE=MyISAM;

PHP

    if ( isset($_POST['json']) ) { 
      mysql_query("INSERT INTO twitter_json ( tj_user_id , tj_json ) VALUES ( ".USERID." , ".mysql_real_escape_string($_POST['data'])." )");
}

PHP(使用存储的JSON 假设你SELECT tj_json FROM twitter_json WHERE tj_user_id = '1' LIMIT 1 那你有

$json_array =  json_decode($row); 

//now try the following:

    echo  $json_array->url; //http://www.aseptik.net

//OR

print "<pre>";
print_r($json_array); // print out everything
print "</pre>";

答案 1 :(得分:1)

如果你想将数据保存到MySQL,你应该在PHP中使用PDO:

<?php
$pdo = new PDO("mysql:host=localhost;dbname=your_database", "root", "");
$stmt = $pdo->prepare("INSERT INTO my_table (name, ...) VALUES (:name, ...)");
$stmt->execute(array(':name' => $twitter['name'], ...));

但是你必须直接从twitter获取你的数据,用PHP:

$twitter = jsondecode(file_get_contents('http://twitter.com/users/' . $username . '.json'));

$pdo = new PDO("mysql:host=localhost;dbname=your_database", "root", "");
$stmt = $pdo->prepare("INSERT INTO my_table (name, ...) VALUES (:name, ...)");
$stmt->execute(array(':name' => $twitter['name'], ...));