2017-07-03 47 views
0

我已經寫了這段代碼。我通過twitter成功獲取數據,但我無法連接到數據庫。我需要將數據存儲到SQL數據庫。 Apache的端口 - 82 MySQL的端口 - 3308 我得到錯誤沒有2005創建Wordpress插件來存儲推文

<?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/[email protected]&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

沒有必要提及端口。

使用此:

define ("DB_HOST","localhost");