2

Consider the following code

if ( isset( $_SESSION['FBID'] )   ) {
    $uid      = $_SESSION['FBID'];
    $sql      = "SELECT *, count(member_nr) AS notifyMe 
                 FROM poolWinners 
                 WHERE member_nr = '$uid'   AND notification ='1'";
    $result = mysql_query($sql);
    while($row=mysql_fetch_array($result)){
       $notification = $row['notifyMe'];
    }//while
      if ( $notification > 0 ) {
        echo '<span class="badge">' . $notification . '</span>';
    } //if
    var_dump($notification);
} //isset( $_SESSION['FBID'] )

The above script returns how many notifications a member has as you can see in image below enter image description here

My Problem

The script is returning the wrong result (wrong number of notifications). Have a look at the table below, the member number appears 3 times in the table so: $notification = $row['notifyMe'] Should = 3 AND NOT 1

What am I missing or doing wrong here? Thanks for reading

2
  • sum up the values in the notification column? as they look to be 1 for each, like this select sumNot from poolWinners where member_nr = '$uid' AND notification = 1; no need to loop over the results, get the sum straight Commented Jun 27, 2015 at 5:54
  • or do select count(member_nr) from poolWinners where member_nr= '$uid' and notification = 1; and use the count instead of the sum, to avoid notification values bigger than 1. again take the full value straight, no need to loop over the result set Commented Jun 27, 2015 at 5:55

5 Answers 5

1

Use

$sql      = "SELECT *, count(*) AS notifyMe 
             FROM poolWinners 
             WHERE member_nr = '$uid'   AND notification ='1'";

Notice count(*) , it will fetch how many records are matching criteria.

And initialize $notification = 0; at the start.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks mate but im getting same problem
Thank you for your effort, but I cant for the live of me figure out why but it is still returning one even with the edit which I thought would solve it, a var_dump confirms $notification value is one (1)......any other suggestions
Define $notification = 0 before while()
Same problem...:-( guess its back to the drawing board for me
Edit question and paste SQL for your table structure
|
1

Have you tried approaching it from this angle

$sql = "SELECT * FROM poolWinners WHERE member_nr = '$uid'   AND notification ='1'";

$result = mysql_query($sql);
$notification = array();
    while($row=mysql_fetch_array($result)){
       $notification[] = $row['notifyMe'];
    }
//an array count on notification should give you the number of elements in the array i.e those that matched the query

$total_count = count($notification);

1 Comment

if you're dealing with a small amount of rows you could try mysql_num_rows to see how many rows were returned
1

In your code the notification will be always one, since it will take only the notifyMe field of last row in the result set.

If you want to get number of notifications, try this

if ( isset( $_SESSION['FBID'] )   ) {
    $uid      = $_SESSION['FBID'];
    $sql      = "SELECT *, count(member_nr) AS notifyMe 
                 FROM poolWinners 
                 WHERE member_nr = '$uid'   AND notification ='1'";
    $result = mysql_query($sql);
    $notification = 0;
    while($row=mysql_fetch_array($result)){
       $notification++; 
       /*
       OR $notification += $row['notifyMe'];
       */
    }//while
      if ( $notification > 0 ) {
        echo '<span class="badge">' . $notification . '</span>';
    } //if
    var_dump($notification);
} //isset( $_SESSION['FBID'] )

Comments

1
$sql      = "SELECT *  
             FROM poolWinners 
             WHERE member_nr = '$uid'   AND notification ='1'";

$result = mysql_query($sql);
$notification = mysql_num_rows($result);

Comments

1
$sql = "SELECT * FROM poolWinners WHERE member_nr = '$uid'   AND notification ='1'";

if ($result=mysqli_query($con,$sql))
  {
  // Return the number of rows in result set
echo "Toal notification".mysqli_num_rows($result);
  }

mysqli_close($con);
?> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.