1

I'm attempting to return the value of a column called "followers_count". On my site I have several users, each with their own count. The update works, in that when you click follow it updates in the database but I wanted to use JSON to display the change without the need to refresh the page. So far the code works, but it only returns the value of followers_count for the last user registered. Anyone know why?

In changes.php:

<?php

require_once 'class.channel.php';

$user_change = new USER();

$stmt = $user_change->runQuery("SELECT followers_count FROM tbl_users");
$stmt->execute();
$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 = $stmt->fetch($query))
{
$follows = $row['followers_count'];
}

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

?>

And in index.php:

<div>
  Channel Adds: <div id="follow_count"></div>
</div>

<script type="text/javascript">

  $(document).ready(function(){
        $.getJSON('changes.php', function(data) {
          $('#follow_count').html(data.followers_count);
        });
      });

</script>
Cordell
  • 77
  • 12

1 Answers1

2

As you loop through the database results, you are replacing the value of $follows each time, so you are only ever storing with the last value.

To add each count to an array you need to change this:

while($row = $stmt->fetch($query)) { 
    $follows = $row['followers_count']; 
}

to this:

while($row = $stmt->fetch($query)) {
    $follows[] = $row['followers_count'];
}

UPDATE:

There is something wrong with your queries:

$seqFollows = $user_change->runQuery( "SELECT currval('followers_count')" );
[...]
$query = $user_change->runQuery($seqFollows);

You are trying run $seqFollows which is a value not a query, so that won't get get the result you are looking for.

You say you are doing $query = $user_change->runQuery($seqFollows); to get the value of $seqFollows, but you already have it. So I suggest you try changing your change.php to the following to pass $seqFollows back as followers_count

<?php
require_once 'class.channel.php';

$user_change = new USER();

$stmt = $user_change->runQuery("SELECT followers_count FROM tbl_users");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);

$currFollows = $row['followers_count'];

$seqFollows = $user_change->runQuery( "SELECT currval('followers_count')" );
$seqFollows->execute();
$row = $seqFollows->fetch(PDO::FETCH_ROW);
$follow_count = $row[0];

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

header('Content-type: application/json');
$array = array('followers_count'=>$follow_count);
echo json_encode($array);
?> 
FluffyKitten
  • 13,824
  • 10
  • 39
  • 52
  • I'm afraid that simply adds 100 to the integer. Since posting my question I determined that it's displaying the data for the last person registered. Whereas I need it to display for each one. I've updated the question to be more concise. – Cordell Aug 31 '17 at 02:07
  • You seem to have something odd going on with `$seqFollows`... First you set it to the value of this query: `$seqFollows = $user_change->runQuery( "SELECT currval('followers_count')" );` but then later, you try to run it as a query, even though it should be a value: `$query = $user_change->runQuery($seqFollows);`. I presume you meant to pass an SQL query instead of a value in the last call? – FluffyKitten Aug 31 '17 at 02:16
  • If I remove the second query the code fails to initiate.. So far everything works, except that I cannot seem to relay the followers_count on a user's page in a way that the code knows it's not the last registered user. To clarify, on my index page (which is essentially a profile page for each user) I have a variable that gets the header index.php?id="userid" and a query that tells the browser the current userID is equal to this. I'm unable to do this with changes.php, so it knows to grab the current user's followers_count value. Do you know how I could go about this? – Cordell Aug 31 '17 at 02:41
  • What are trying to get from the database when you use `$query = $user_change->runQuery($seqFollows);`? I'm not saying to remove that query, just that it doesn't look right so maybe that's whats causing the problem. You are not passing in an SQL query, so it can't be returning the right result. – FluffyKitten Aug 31 '17 at 02:46
  • It's supposed to get the value of $seqFollows which = $user_change->runQuery( "SELECT currval('followers_count')" ); .. It appears to do just that and the values are showing up right in the browser as the follow button is clicked (on last users page). Only it shows the same followers_count on every page, specifically that of the last registered user. – Cordell Aug 31 '17 at 03:06
  • But you already have the value of `$seqFollows`, so why would you be getting it again? Trying to run a query on a value is incorrect and won't work properly, so whether it's causing this issue or not will cause problems at some point. I'd suggest trying to debug your code by sending back a message through the ajax, see: https://stackoverflow.com/questions/21897398/how-do-i-debug-jquery-ajax-calls – FluffyKitten Aug 31 '17 at 03:12
  • There are no ajax errors being returned in in the network tab or the error log, site side. From what I can tell (having checked the users table in phpmyadmin, and from experimenting) my code does not know which followers_count to look at (seeing as there are currently three users registered), and so it simply retrieves the count from the last user. As I press the follow button on this user's page the count goes up, and results are displayed properly in the database as well. I'm royally stuck on this, as I have no idea how to query the database so it knows whose count to display. – Cordell Aug 31 '17 at 03:23
  • There is nothing wrong with the syntax of your code so you won't get any errors. The problem is with the logic, which will only give an error if you explicitly check and throw one. The problem is retrieving the result as you say, and your last query cannot work for the reasons I've explained. Why are you trying to get `$seqFollows` when you already have it? Also, what is `currFollows` and why do you exit if it matches `seqFollows`? I'm trying to understand the logic behind your code so I can find where you're going wrong. – FluffyKitten Aug 31 '17 at 03:27
  • I honestly thought the same thing. However, without this second query the code simply fails. Unless there is a better way to go about it, which I'm sure there is. currFollows is meant to be the current followers_count, and seqFollows is supposed to be the value if updated through clicking a follow button. If they are the same then no call to JSON is made. – Cordell Aug 31 '17 at 03:37
  • I've updated my answer with new code for changes.php - see if that works. Just to be clear, its the third query I'm questioning. If you already have `seqFollows` then is that not the value you want to pass back as "followers_count"? I don't understand why you wouldn't want to run the complete the ajax call if crr and seq are the same, but I think don't think its relevant to the problem anyway (I presume you've checked that already) – FluffyKitten Aug 31 '17 at 03:45
  • Thank you. I've made the changes however changes.php now returns as: {"followers_count":{"queryString":"SELECT currval('followers_count')"}} – Cordell Aug 31 '17 at 03:57
  • Wait a minute... does `runQuery` not actually execute the query? Ok, I've updated the changes.php in the answer - try that... – FluffyKitten Aug 31 '17 at 04:02
  • You're right. Ok, so I think this might explain a lot: [31-Aug-2017 04:07:47 UTC] PHP Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION livechannels.currval does not exist in /home/iamlive/public_html/changes.php:13 Stack trace: #0 /home/iamlive/public_html/changes.php(13): PDOStatement->execute() #1 {main} thrown in /home/iamlive/public_html/changes.php on line 13 .. Is currval not a built-in PDO function? – Cordell Aug 31 '17 at 04:11
  • It depends on what database are you using. Also the error is referencing *livechannels*.currval so it could be something to do with the classes you're using? I'm pretty sure if you get that query to run you will be on the road to fixing the problem, but I'm afraid I'm not at all familiar with the classes you are using to do access your database so I think this is where I have to leave you :) – FluffyKitten Aug 31 '17 at 04:29
  • I'm sure I can solve it from here. Thank you for your help. – Cordell Aug 31 '17 at 04:32