Jump to content

Archived

This topic is now archived and is closed to further replies.

DaveTomneyUK

Count Trouble

Recommended Posts

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.
[code]
$row = mysql_fetch_row(mysql_query("SELECT COUNT(*) AS numrows FROM table_news WHERE active='yes'"));
$numrows = $row['numrows'];
[/code]
I tried making a function of my own but it wont work it returns nothing

[code]
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'");
[/code]

Share this post


Link to post
Share on other sites
I don't understand, are you writing an object or function?
in any case you can do either:

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

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

Share this post


Link to post
Share on other sites
[!--quoteo(post=383860:date=Jun 14 2006, 10:37 AM:name=DaveTomneyUK)--][div class=\'quotetop\']QUOTE(DaveTomneyUK @ Jun 14 2006, 10:37 AM) [snapback]383860[/snapback][/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.
[code]
$row = mysql_fetch_row(mysql_query("SELECT COUNT(*) AS numrows FROM table_news WHERE active='yes'"));
$numrows = $row['numrows'];
[/code]
I tried making a function of my own but it wont work it returns nothing

[code]
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'");
[/code]
[/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 =''

[b]function count($table, $where) {[/b]

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

[b]global $this; [/b]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.