DaveTomneyUK Posted June 14, 2006 Share Posted June 14, 2006 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 Link to comment https://forums.phpfreaks.com/topic/11989-count-trouble/ Share on other sites More sharing options...
joquius Posted June 14, 2006 Share Posted June 14, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/11989-count-trouble/#findComment-45594 Share on other sites More sharing options...
.josh Posted June 14, 2006 Share Posted June 14, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/11989-count-trouble/#findComment-45608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.