Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Count

Options
  • 16-08-2007 2:47pm
    #1
    Registered Users 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 Posts: 68,317 ✭✭✭✭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 Posts: 2,934 ✭✭✭egan007


    or...

    $totalday=mysql_num_rows($getdays)


  • Closed Accounts Posts: 518 ✭✭✭danbhala


    or...

    $totalday=sizeof($getdays)


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


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


  • Advertisement
  • Registered Users 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 Posts: 39,822 Mod ✭✭✭✭Seth Brundle


    Can you not use COUNT() in your SQL?


  • Registered Users 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 Posts: 39,822 Mod ✭✭✭✭Seth Brundle




  • Registered Users 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 Posts: 68,317 ✭✭✭✭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 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