Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP Count

  • 16-08-2007 02:47PM
    #1
    Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭


    I'm trying to count how many rows I have in a table that have similar likes. This is my script:
    [PHP]$getdays = mysql_query("SELECT day FROM archive WHERE day='$_POST[day]'");
    $dayarr = mysql_fetch_array($getdays);
    $totalday = count($dayarr);
    echo $totalday;[/PHP]
    The echo isn't giving me the correct number.

    e.g. I have 4 rows where the days are the same and I being told there are 2?!

    Is my script A-wall?

    Thanks,
    S.


Comments

  • Closed Accounts Posts: 161 ✭✭nude_hamster


    Seachmall wrote:
    I'm trying to count how many rows I have in a table that have similar likes. This is my script:
    [PHP]$getdays = mysql_query("SELECT day FROM archive WHERE day='$_POST[day]'");
    $dayarr = mysql_fetch_array($getdays);
    $totalday = count($dayarr);
    echo $totalday;[/PHP]
    The echo isn't giving me the correct number.

    e.g. I have 4 rows where the days are the same and I being told there are 2?!

    Is my script A-wall?

    Thanks,
    S.

    I know this is kinda obvious question but...
    Have you tested that SQL statement, maybe the query is incorrect?


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    mysql_fetch_array() returns a single row from the result, not the entire result. In this case, the count() function returning 2 is completely correct.

    There are a number of things you can do for this, but my personal favourite is mysql_affected_rows()

    This function will return the number of rows returned/changed/deleted in the last query.


  • Registered Users, Registered Users 2 Posts: 2,934 ✭✭✭egan007


    or...

    $totalday=mysql_num_rows($getdays)


  • Closed Accounts Posts: 518 ✭✭✭danbhala


    or...

    $totalday=sizeof($getdays)


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    sizeof() won't work. It's just an alias for count().


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭Seachmall


    So what do I use to count the number of unaffected rows? I'm trying to count all the rows in the table before I change them. Will mysql_affected_rows work for this?

    Thanks,
    S.


  • Moderators, Politics Moderators, Paid Member Posts: 44,033 Mod ✭✭✭✭Seth Brundle


    Can you not use COUNT() in your SQL?


  • Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭Seachmall


    kbannon wrote:
    Can you not use COUNT() in your SQL?
    as in COUNT * FROM etc..?

    Thats doesn't appear to be working but I've a feeling I'm doing it wrong. I'm kinda of embarrassed I forgot the COUNT command but ya live, ya learn etc.:D

    Anyway, Thanks,
    S.


  • Moderators, Politics Moderators, Paid Member Posts: 44,033 Mod ✭✭✭✭Seth Brundle




  • Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭Seachmall


    So this is what I have now but it still outputs 2
    [PHP]$getdays = mysql_query("SELECT day FROM archive WHERE day='$_POST[day]'");
    $dayarr = mysql_fetch_array($getdays);
    print count($dayarr);[/PHP]

    Can somebody give me the script they know works so I can see if I'm anywhere near. I've been stuck on this part for weeks and I'm so close to finishing my program. Please, I need help.:(
    Thanks,
    S.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    That's exactly the same script as you posted in the first place :confused:

    One way of doing it:
    [php]
    $getdays = mysql_query("SELECT day FROM archive WHERE day='$_POST[day]'");
    print mysql_affected_rows();
    [/php]


  • Registered Users, Registered Users 2 Posts: 7,041 ✭✭✭Seachmall


    seamus wrote:
    That's exactly the same script as you posted in the first place :confused:
    I've been trying everything (except the right one) and retrying them. Sorry for the repost.
    seamus wrote:
    One way of doing it:
    [php]
    $getdays = mysql_query("SELECT day FROM archive WHERE day='$_POST[day]'");
    print mysql_affected_rows();
    [/php]

    Thats it!:) I havent heard of that command before but I searched it up and it looks like it could definetly come in handy in the future.
    Thanks,
    S.


Advertisement