Jump to content

Count Trouble


DaveTomneyUK

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]
Link to comment
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
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.