Jump to content

Loop the message from db to facebook


dumb2champ

Recommended Posts

Hello there..

 

I have a problem when loading comment from my database..

 

Here are working scripts which does not query the message/comment from database..

    <?php

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "responder";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    } 

    //query the user table
    $sql = "SELECT * FROM user";
    $result = $conn->query($sql);

    //get comment from array
    $comment = $_GET['comment'] + 1;

    //I want to replace below array comment with my database comment
    $ar = array(
                  "Hello there.",
                  "How are you?"
          );

    require_once("facebook.php");

    $config = array();
    $config['appId'] = 'myapp';
    $config['secret'] = 'mysecret';
    $config['fileUpload'] = false;

    $fb = new Facebook($config);

    if ($result->num_rows > 0) {
      foreach ($result as $key) {

    $params = array(

	//long-live-token
        "access_token" => $key['offense_token'],
        
        //comment
        //need to replace this from database
        "message" => $ar[$comment],
     );

    if($comment<10)
    {
     try {

      $ret = $fb->api('/my-comment-id/comments', 'POST', $params);

      echo 'Successfully posted to Facebook';
      sleep(5);
      header('Location: set-comment.php?comment='.$comment);
    } catch(Exception $e) {
      echo $e->getMessage();
    }
    }
    else{
	echo "done"."<br>"; 
    }
    }
    }

    ?>

Above scripts working fine without querying from my database

 

So my issues now, how do I query and loop each comment from database?

 

Here my comment/message table..

HznQfH7.png?1

 

TQ

Link to comment
https://forums.phpfreaks.com/topic/300930-loop-the-message-from-db-to-facebook/
Share on other sites

Do another query to that table

 

Written so will only populate array if are results, otherwise is an empty array.

I didn't know the actual name of your table so change that.

$ar  = array();
$sql = "SELECT offense_comment FROM comments_table";
if ($result = $conn->query($sql)) {
    
    if (mysqli_num_rows($result) > 0) {
        
        while ($row = $result->fetch_assoc()) {
            
            $ar[] = $row['offense_comment'];
            
        }
        
    }
    
}

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.