0

I'm attempting to return the value of a database integer called "followers_count", so that the user needs not refresh the webpage to see the change. However, my code does not work. Any help is appreciated.

What I have for the script:

<script type="text/javascript">
  $(document).ready(function() {
    $.getJSON('change.php', function(data) {
      $('#follow_count').html(data.followers_count);
    });
  });
</script>

In change.php: (Edited per comments, but getting error "followers_count=null")

<?php

require_once 'dbconfig.php';
require_once 'class.channel.php';

$user_change = new USER();

$userID = ( isset( $_GET['id']) && !empty( $_GET['id'] ) ) ? trim($_GET['id']) : '' ;


$stmt = $user_change->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$userID));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

$currFollows = $row['followers_count'];

$seqFollows = $user_change->runQuery( "SELECT currval('followers_count')" );

if ($seqFollows == $currFollows){
    exit(0);
}

//$query = $user_change->runQuery($seqFollows);

while($row = mysqli_fetch_array($seqFollows))
{
$follows = $row['followers_count'];
}

header('Content-type: application/json');
$array = array('followers_count'=>$follows);
echo json_encode($array);

?>

The HTML:

 <div>
   Channel Adds: <div id="follow_count"></div>
 </div>
Cordell
  • 77
  • 12
  • Try opening the `change.php` file directly in your browser and see if it outputs any errors. – Martin Heralecký Aug 30 '17 at 23:08
  • Where is your first call of ** $currFollows = $row['followers_count']; ** getting it's $row data from? – Jamie M Aug 30 '17 at 23:11
  • [30-Aug-2017 23:10:22 UTC] PHP Fatal error: Uncaught Error: Using $this when not in object context in /home/iamlive/public_html/changes.php:8 Stack trace: #0 {main} thrown in /home/iamlive/public_html/changes.php on line 8 – Cordell Aug 30 '17 at 23:11
  • What is `$this`? Lines 8 and 14. – Aydin4ik Aug 30 '17 at 23:14
  • @Jamie, on the index.php I have a query to table (users) that contains this column. Would that be an issue? – Cordell Aug 30 '17 at 23:15
  • @Aydin, in class.channel.php I have the following function: public function __construct() { $database = new Database(); $db = $database->dbConnection(); $this->conn = $db; } – Cordell Aug 30 '17 at 23:16
  • And in which class is this method sitting? You have to reference that class, `$this` will not reference that method outside that class. Made it into an answer, can provide more info if you tell us more about the class.channel.php file or that particular class where $database, etc are. – Aydin4ik Aug 30 '17 at 23:23
  • After referencing the class, all seems to be working except I'm getting the error "followers_count=null". Not sure what I'm doing wrong, as the followers_count does in fact update, just not in real time. – Cordell Aug 30 '17 at 23:44

1 Answers1

0
  1. On lines 8 and 14 in change.php, you are referencing $this. It appears that what you are trying to reference is a method in a class in another file. In OOP context, $this references the current object (more info). You have to make sure you are using the correct reference to the class/method you are trying to call.

  2. $follows variable is declared within the while loop. As such, it expires within that loop. You have to declare it outside the loop to be able to use it afterwards.

Aydin4ik
  • 1,782
  • 1
  • 14
  • 19
  • Thank you for the response. I have now referenced the class, but am getting the error that followers_count is null. (Updated the question with new code) – Cordell Aug 30 '17 at 23:32
  • Getting current errors in PHP: [30-Aug-2017 23:48:57 UTC] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in /home/iamlive/public_html/changes.php on line 25 [30-Aug-2017 23:48:57 UTC] PHP Notice: Undefined variable: follows in /home/iamlive/public_html/changes.php on line 31 – Cordell Aug 30 '17 at 23:50