Jump to content

Loop the message from db to facebook


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..


    $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?"


    $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(

        "access_token" => $key['offense_token'],
        //need to replace this from database
        "message" => $ar[$comment],

     try {

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

      echo 'Successfully posted to Facebook';
      header('Location: set-comment.php?comment='.$comment);
    } catch(Exception $e) {
      echo $e->getMessage();
	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..




Link to comment
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'];
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • 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.