Jump to content


Photo

Count Trouble


  • Please log in to reply
2 replies to this topic

#1 DaveTomneyUK

DaveTomneyUK
  • Members
  • PipPip
  • Member
  • 13 posts
  • LocationUK, England

Posted 14 June 2006 - 03:37 PM

Hi,

I am trying to count my tables using the COUNT(*) but having some trouble when making a custom function anyone know why it wont return nothing.

I currently use this old code which works but it looks like old bad code.
$row = mysql_fetch_row(mysql_query("SELECT COUNT(*) AS numrows FROM table_news WHERE active='yes'"));
$numrows = $row['numrows'];
I tried making a function of my own but it wont work it returns nothing

function count($table, $where='') {
    if ($where != '') $where = "WHERE $where";
        $query_id = $this->mysql_query("SELECT COUNT(*) FROM $table $where", false, true);
        list($count) = $this->mysql_fetchrow($query_id, MYSQL_ASSOC);
        $this->mysql_freeresult($query_id);
        return $count;
    }

$numrows = count('table_news', "active='yes'");


#2 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 03:56 PM

I don't understand, are you writing an object or function?
in any case you can do either:

$query = mysql_query ("SELECT COUNT(`field`) FROM `table`") or die (mysql_error ());
$num_rows = mysql_result ($query, 0);
or

$query = mysql_query ("SELECT `field` FROM `table`") or die (mysql_error ());
$num_rows = mysql_num_rows ($query);
option 1 is much better if you can manage it
z..z..z..z..z..z..z..z..

#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 June 2006 - 04:30 PM

[!--quoteo(post=383860:date=Jun 14 2006, 10:37 AM:name=DaveTomneyUK)--][div class=\'quotetop\']QUOTE(DaveTomneyUK @ Jun 14 2006, 10:37 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi,

I am trying to count my tables using the COUNT(*) but having some trouble when making a custom function anyone know why it wont return nothing.

I currently use this old code which works but it looks like old bad code.
$row = mysql_fetch_row(mysql_query("SELECT COUNT(*) AS numrows FROM table_news WHERE active='yes'"));
$numrows = $row['numrows'];
I tried making a function of my own but it wont work it returns nothing

function count($table, $where='') {
    if ($where != '') $where = "WHERE $where";
        $query_id = $this->mysql_query("SELECT COUNT(*) FROM $table $where", false, true);
        list($count) = $this->mysql_fetchrow($query_id, MYSQL_ASSOC);
        $this->mysql_freeresult($query_id);
        return $count;
    }

$numrows = count('table_news', "active='yes'");
[/quote]
your query never executes becuse you set $where equal to '' when in your function argument. then you have your if statement executing only if it is != to ''. So take out the =''

function count($table, $where) {

after that, you are probably going to run into troubles with your $this variable. Where did you declare this object? Where is the class? you did not pass it through the function argument. I assume that you have a class somewhere else, and made the $this somewhere else. At the very least, I think you need to do like

global $this;

inside that function, so that it can use it within it's scope.

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users