我写了这段代码。我通过Twitter成功获取数据,但我无法连接到数据库。我需要将数据存储到SQL DB。 Apache Port- 82 MySQL端口 - 3308 我得到的错误是2005年
<?php include "twitteroauth/twitteroauth.php";
define ("DB_USER","root");
define ("DB_PASSWORD","root");
define ("DB_HOST","localhost:3308");
define ("DB_NAME","rwh-1");
define ("DB_SOCKET","/Applications/MAMP/tmp/mysql/mysql.sock");
/**
*Plugin Name: Basic Plugin
*Plugin URI: http://www.google.com
*Author: Vivek Kumar
*Description: Displaying twitter's handle tweet.
*Version: 1.0
*/
$dbc =mysqli_connect("DB_HOST","DB_USER","DB_PASSWORD");
function do_something(){
//do something
?>
<form action="" method="POST">
<label>Search :<input type="text" name="keyword"/></label>
</form>
<?php
$consumer = "****";
$consumercecret="****";
$accesstoken="****";
$accesstokensecret="****";
$twitter =new TwitterOAuth($consumer,$consumercecret,$accesstoken,$accesstokensecret);
$tweets = $twitter->get('https://api.twitter.com/1.1/search/tweets.json?q=@imsrk&result_type=recent');
if(isset($_POST['keyword'])){
$tweets=$twitter->get('https://api.twitter.com/1.1/search/tweets.json?q='.$_POST['keyword'].'&lang=en&result_type=recent&count=100');
foreach($tweets as $tweet){
foreach($tweet as $t)
{
if($t->text!=null)
{
$query="INSERT INTO handletweet (name, tweet) VALUES (\"".$t->user->name."\",\"".$t->text."\")";
if(!$dbc)
echo ('Failed to connect to Mysql: '.mysqli_connect_errno($dbc).' <br><br>');
$stmt =mysqli_prepare($dbc,$query);
mysqli_stmt_execute($stmt);
$affected_rows=mysqli_stmt_affected_rows($stmt);
if($affected_rows == 1)
echo 'Name: '.$t->user->name. ' 1' .'<br>' .$t->text.'<br><br>';
else
echo 'Name: '.$t->user->name.' 0' .'<br>' .$t->text.'<br><br>';
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}
}
}
}
}
add_action('the_content','do_something');
?>
答案 0 :(得分:0)
无需提及端口。
使用此:
define ("DB_HOST","localhost");