Division by Zero. WTF

Create the Class Room, Upload the Image.  Then go to view the Class Room List and we get this:

Warning: Division by zero in /home/mydatery/public_html/inc/classes/BxDolClassRooms.php on line 1839

Database query error
Query:
SELECT `Clrooms`.*, UNIX_TIMESTAMP( `Clrooms`.`created`) AS 'created_UTS', `ClroomsCateg`.`Name` AS `categName`, `ClroomsCateg`.`Uri` AS `categUri`, COUNT( `ClroomsMembers`.`memberID` ) AS `membersCount`, `ClroomsGallery`.`seed`, `ClroomsGallery`.`ext` AS `thumbExt` FROM `Clrooms` INNER JOIN `ClroomsCateg` ON `ClroomsCateg`.`ID` = `Clrooms`.`categID` LEFT JOIN `ClroomsMembers` ON (`ClroomsMembers`.`clroomID` = `Clrooms`.`ID` AND `ClroomsMembers`.`status`='Active') LEFT JOIN `ClroomsGallery` ON (`Clrooms`.`thumb` = `ClroomsGallery`.`ID`) WHERE (`Clrooms`.`status` = 'Active') GROUP BY `Clrooms`.`ID` ORDER BY `membersCount` DESC, `Clrooms`.`ID` DESC LIMIT -0,
Mysql error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-0,' at line 18
With this Debug:
Found error in file /home/mydatery/public_html/inc/classes/BxDolClassRooms.php
at line 1870. Called db_res function with erroneous argument #0

Debug backtrace:
Array
(
    [1] => Array
        (
            [file] => /home/mydatery/public_html/inc/classes/BxDolClassRooms.php
            [line] => 1870
            [function] => db_res
            [args] => Array
                (
                    [0] => 
			SELECT
				`Clrooms`.*,
				UNIX_TIMESTAMP( `Clrooms`.`created`) AS 'created_UTS',
				`ClroomsCateg`.`Name` AS `categName`,
				`ClroomsCateg`.`Uri` AS `categUri`,
				COUNT( `ClroomsMembers`.`memberID` ) AS `membersCount`,
				`ClroomsGallery`.`seed`,
				`ClroomsGallery`.`ext` AS `thumbExt`
			FROM `Clrooms`
			INNER JOIN `ClroomsCateg` ON `ClroomsCateg`.`ID` = `Clrooms`.`categID`
			LEFT JOIN `ClroomsMembers`
				ON (`ClroomsMembers`.`clroomID` = `Clrooms`.`ID` AND `ClroomsMembers`.`status`='Active')
			LEFT JOIN `ClroomsGallery`
				ON (`Clrooms`.`thumb` = `ClroomsGallery`.`ID`)
			WHERE (`Clrooms`.`status` = 'Active')
			GROUP BY `Clrooms`.`ID`
			ORDER BY `membersCount` DESC, `Clrooms`.`ID` DESC
			LIMIT -0, 
			
                )

        )

    [2] => Array
        (
            [file] => /home/mydatery/public_html/inc/classes/BxDolClassRooms.php
            [line] => 58
            [function] => PageCompClroomsSearchResults
            [class] => BxDolClassRooms
            [type] => ->
            [object] => BxDolClassRooms Object
                (
                    [sCurrFile] => clr.php
                    [bAdminMode] => 
                    [bUseFriendlyLinks] => 
                    [sSubClrPath] => clrooms/
                    [sClrPath] => /home/mydatery/public_html/clrooms/
                    [sClrGalPath] => /home/mydatery/public_html/clrooms/gallery/
                    [sSpacerIcon] => http://mydatery.com/templates/base/images/icons/spacer.gif
                )

            [args] => Array
                (
                    [0] => 
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => membersCount
                    [6] => 1
                )

        )

    [3] => Array
        (
            [file] => /home/mydatery/public_html/clr.php
            [line] => 237
            [function] => GenIndexPageOfClrooms
            [class] => BxDolClassRooms
            [type] => ->
            [object] => BxDolClassRooms Object
                (
                    [sCurrFile] => clr.php
                    [bAdminMode] => 
                    [bUseFriendlyLinks] => 
                    [sSubClrPath] => clrooms/
                    [sClrPath] => /home/mydatery/public_html/clrooms/
                    [sClrGalPath] => /home/mydatery/public_html/clrooms/gallery/
                    [sSpacerIcon] => http://mydatery.com/templates/base/images/icons/spacer.gif
                )

            [args] => Array
                (
                )

        )

)
Now here is the string of code from 1839 BxDolClassRooms.php
 function PageCompClroomsSearchResults( $sKeyword, $sSearchby, $iCategID, $sCountry, $sCityVal, $sSortby, $isTopClroomsPage = false ) {
  global $oTemplConfig;
  global $site;
  global $tmpl;
  global $aPreValues;
  $sHtmlRet = '';
  $date_format_php = getParam('php_date_format');
  if( $sSortby == 'created' or $sSortby == 'membersCount' )
   $sortOrder = 'DESC';
  else
   $sortOrder = 'ASC';
  $aQueryWhere = array(); //array will contain search conditions combined by AND
  if( $sKeyword ) {
   if( $sSearchby == 'name' )
    $aQueryWhere[] = "UPPER(`Clrooms`.`Name`) LIKE '%{$sKeyword}%'";
   else
    $aQueryWhere[] = "(UPPER(`Clrooms`.`Name`) LIKE '%{$sKeyword}%') OR (UPPER(`Clrooms`.`About`) LIKE '%{$sKeyword}%') OR (UPPER(`Clrooms`.`Desc`) LIKE '%{$sKeyword}%')";
  }
  if( $iCategID )
   $aQueryWhere[] = "`Clrooms`.`categID`='{$iCategID}'";
  if( $sCountry )
   $aQueryWhere[] = "`Clrooms`.`Country`='{$sCountry}'";
  if( $sCityVal )
   $aQueryWhere[] = "UPPER(`Clrooms`.`City`) LIKE '%{$sCityVal}%'";
  $aQueryWhere[] = "`Clrooms`.`status` = 'Active'";
  $sQueryWhere = "WHERE (" . implode( ") AND (", $aQueryWhere ) . ")";
  if( $isTopClroomsPage )
   $SRdbTitle = _t( '_Top Clrooms' );
  else
   $SRdbTitle = _t( '_Clrooms search results' ); //SearchResultDesignBoxTitle
  $arrNum = db_arr( "SELECT COUNT(`ID`) FROM `Clrooms` {$sQueryWhere}" );
  $iTotalNum = (int)$arrNum[0];
  if( $iTotalNum > 0 ) {
   $iPerPage = $oTemplConfig -> iClroomsSearchResPerPage;
   $iPagesNum = ceil( $iTotalNum / $iPerPage );
   $iPage = (int)$_REQUEST['page'];
   if( $iPage < 1 )
    $iPage = 1;
   if( $iPage > $iPagesNum )
    $iPage = $iPagesNum;
   $iSqlFrom = ( ( $iPage - 1 ) * $iPerPage );
   $sQuery = "
    SELECT
     `Clrooms`.*,
     UNIX_TIMESTAMP( `Clrooms`.`created`) AS 'created_UTS',
     `ClroomsCateg`.`Name` AS `categName`,
     `ClroomsCateg`.`Uri` AS `categUri`,
     COUNT( `ClroomsMembers`.`memberID` ) AS `membersCount`,
     `ClroomsGallery`.`seed`,
     `ClroomsGallery`.`ext` AS `thumbExt`
    FROM `Clrooms`
    INNER JOIN `ClroomsCateg` ON `ClroomsCateg`.`ID` = `Clrooms`.`categID`
    LEFT JOIN `ClroomsMembers`
     ON (`ClroomsMembers`.`clroomID` = `Clrooms`.`ID` AND `ClroomsMembers`.`status`='Active')
    LEFT JOIN `ClroomsGallery`
     ON (`Clrooms`.`thumb` = `ClroomsGallery`.`ID`)
    {$sQueryWhere}
    GROUP BY `Clrooms`.`ID`
    ORDER BY `{$sSortby}` {$sortOrder}, `Clrooms`.`ID` DESC
    LIMIT {$iSqlFrom}, {$iPerPage}
   ";
   $vResClrooms = db_res( $sQuery );
   $iNum alt= mysql_num_rows( $vResClrooms );
   $iShowingFrom = $iSqlFrom + 1;
   $iShowingTo   = $iSqlFrom + $iNumOnPage;
   $sShowingResults = _t( '_Showing results:', $iShowingFrom, $iShowingTo, $iTotalNum );
   if( $iPagesNum > 1 and !$isTopClroomsPage ) {
    $sPagesUrl = ";";
    $pages alt= "switchClroomsSearchPage({page}); return false;";
    $sGenPaginati alt= genPagination( $iPagesNum, $iPage, $sPagesUrl, $pagesOnclick );
   }
   $sRowTmpl = file_get_contents( BX_DIRECTORY_PATH_ROOT . "templates/tmpl_{$tmpl}/searchrow_clroom.html");
   $sBreadCrumbs = '';
   if( $iCategID ) {
    $arrCateg = db_arr( "SELECT `Name` FROM `ClroomsCateg` WHERE `ID`='{$iCategID}'" );
    if( $arrCateg['Name'] ) {
     $sCategName = _t( '_Category' ).': '.htmlspecialchars_adv( $arrCateg['Name'] );
     $SRdbTitle = $sCategName;
     $sClroomsUrl = $this->bUseFriendlyLinks ? 'clrooms/all' : $this->sCurrFile ;
     $sBreadCrumbs = <<<EOJ
<div class="clrooms_breadcrumbs">
 <a href="{$site['url']}">{$site['title']}</a> /
 <a href="{$site['url']}{$sClroomsUrl}">__Clrooms__</a> /
 <span class="active_link">{$sCategName}</span>
</div>
EOJ
Note that I have colored the offending line as red.  So what in the world is this trying to divide by zero?
Quote · 16 Apr 2010

Probably not the line then. Incorrect reporting of the error location. Is that a division I see very close to that statement?

$iPagesNum = ceil( $iTotalNum / $iPerPage );

Quote · 16 Apr 2010

 

Probably not the line then. Incorrect reporting of the error location. Is that a division I see very close to that statement?

$iPagesNum = ceil( $iTotalNum / $iPerPage );

 

 I see that and thought about it too CBass, but this is where I'm getting totally confused on this.

We have the error line that is reported as an if total is greater than zero then do the division.  In this instance the total is zero so shouldn't it be doing nothing?  Mind you, when there are NO Class Rooms created at all, just the categories then the page resolves correctly.  It is when we have 1 or more categories that it's then erring out like it's pulling a zero from someplace.

 

Now in the end the formula is:

 

Total Number Divided by the Per Page variable.  Have I missed inputting the per page variable somewhere since it's saying divided by zero?  I mean, if it was 10 per page then it would figure in easily, correct?  I've looked at the DB's and see nothing that can Limit this per page nor can I figure out why it's not completing the calculations.

Shouldn't the

 

 

if( $iPage < 1 )
    $iPage = 1;
   if( $iPage > $iPagesNum )
    $iPage = $iPagesNum;

   $iSqlFrom = ( ( $iPage - 1 ) * $iPerPage );

 

Be taking care of this?

Quote · 16 Apr 2010

I don't really see anything wrong. You may need to echo some values out if it will work on this page.

$iPagesNum = ceil( $iTotalNum / $iPerPage );

A division by 0 would occure here if $iPerPage is 0

So is the line that sets that $iPerPage = $oTemplConfig -> iClroomsSearchResPerPage; returning 0?

You could insert a echo $iPerPage and a exit() after that to see what it gets set to.

I often use echo and exit blocks whne i have problems with something so the script shows me things and exits.




https://www.deanbassett.com
Quote · 17 Apr 2010

This is friggin' hilarious.  If I remove

 

LIMIT from the bottom of the query the page will resolve, but it still has the Warning Division by Zero going on at the top.  Nice appearance for the page but it's progress.  LOL

Now, when I added in your echo statement and exit it kills the error but then it just gives me a blank page like a php error.  The dang thing gives nothing on this.  It literally wants to give up nothin' on the solution.  The thing is, this happens multiple times in the file depending on where we're going (the link specifically) and each time I remove LIMIT it fixes the page and still throws the error.  I guess that means it kinda fixes it. 

Okay, here is how I put it in, maybe I'm to tired and not seein' this.

Now, I just had a brain fart I'm sure but I'm thinkin' this might work.  We know in this case that the iPerPage is errin' out at the 1 level.  At Zero it actually works, it's when it's 1 or more we get this error.  So, what if we did this using if variable, then else and if again?  Something like this for instance:


 

if( $iTotalNum > 0 ) {
   $iPerPage = $oTemplConfig -> iClroomsearchResPerPage;
   echo $iPerPage;
   exit();
   $iPagesNum = ceil( $iTotalNum / $iPerPage );

   $iPage = (int)$_REQUEST['page'];
   }
   else
   {
   if( $iPage < 1 )
    $iPage = 1;
   }
   else
   {
   if( $iPage > $iPagesNum )
    $iPage = $iPagesNum;

   $iSqlFrom = ( ( $iPage - 1 ) * $iPerPage );

   $sQuery = "
    SELECT
     `Clrooms`.*,
     UNIX_TIMESTAMP( `Clrooms`.`created`) AS 'created_UTS',
     `ClroomsCateg`.`Name` AS `categName`,
     `ClroomsCateg`.`Uri` AS `categUri`,
     COUNT( `ClroomsMembers`.`memberID` ) AS `membersCount`,
     `ClroomsGallery`.`seed`,
     `ClroomsGallery`.`ext` AS `thumbExt`
    FROM `Clrooms`
    INNER JOIN `ClroomsCateg` ON `ClroomsCateg`.`ID` = `Clrooms`.`categID`
    LEFT JOIN `ClroomsMembers`
     ON (`ClroomsMembers`.`clroomID` = `Clrooms`.`ID` AND `ClroomsMembers`.`status`='Active')
    LEFT JOIN `ClroomsGallery`
     ON (`Clrooms`.`thumb` = `ClroomsGallery`.`ID`)
    {$sQueryWhere}
    GROUP BY `Clrooms`.`ID`
    ORDER BY `{$sSortby}` {$sortOrder}, `Clrooms`.`ID` DESC
    LIMIT
   ";

   $vResClrooms = db_res( $sQuery );

   $iNum alt= mysql_num_rows( $vResClrooms );
   $iShowingFrom = $iSqlFrom + 1;
   $iShowingTo   = $iSqlFrom + $iNumOnPage;

   $sShowingResults = _t( '_Showing results:', $iShowingFrom, $iShowingTo, $iTotalNum );

   if( $iPagesNum > 1 and !$isTopClroomsPage ) {
    $sPagesUrl = ";";
    $pages alt= "switchClroomsSearchPage({page}); return false;";
    $sGenPaginati alt= genPagination( $iPagesNum, $iPage, $sPagesUrl, $pagesOnclick );
   }
  }


I'll give this a try and report back what it does or doesn't do for the problem.  Should be fun. 

Quote · 17 Apr 2010

if( $iTotalNum > 0 ) {
$iPerPage = $oTemplConfig -> iClroomsearchResPerPage;
echo $iPerPage;
exit();

If you are getting nothing there when it exits, then my guess is $iPerPage is getting set to NULL there.


https://www.deanbassett.com
Quote · 17 Apr 2010

Okay, that did not work.  I just took a shot in the dark and removed the

 $iPagesNum = ceil( $iTotalNum / $iPerPage );

And it worked with no errors.  But somehow I'm thinkin' this is going to create more issues.  Note I also removed the:

 

LIMIT {$iSqlFrom}, {$iPerPage}

from the end of the query too.  I tried reversing the divide and that did no good. 

 

Here's a question that might help.  In inc/classes/BxDolGroups.php they have the same exact code we're working with here structurally and it works perfectly.  Where is Groups pulling this number from to make the problem work?  Perhaps I'm just missing something in the DB that I'm not seeing.

Quote · 17 Apr 2010

Your not going to believe this Deano.  But look at this code:

 


$iTotalNum = (int)$arrNum[20];
  if( $iTotalNum > 0 ) {
   $iPerPage = $oTemplConfig -> iClroomsearchResPerPage;
   $iPage = (int)$_REQUEST['page'];
   $iPagesNum = ceil( $iTotalNum / $iPerPage );   
   if( $iPage = 1 )
    $iPage = 1;
   if( $iPage > $iPagesNum )
    $iPage = $iPagesNum;

   $iSqlFrom = ( ( $iPage - 1 ) * $iPerPage );

   $sQuery = "
    SELECT
     `Clrooms`.*,
     UNIX_TIMESTAMP( `Clrooms`.`created`) AS 'created_UTS',
     `ClroomsCateg`.`Name` AS `categName`,
     `ClroomsCateg`.`Uri` AS `categUri`,
     COUNT( `ClroomsMembers`.`memberID` ) AS `membersCount`,
     `ClroomsGallery`.`seed`,
     `ClroomsGallery`.`ext` AS `thumbExt`
    FROM `Clrooms`
    INNER JOIN `ClroomsCateg` ON `ClroomsCateg`.`ID` = `Clrooms`.`categID`
    LEFT JOIN `ClroomsMembers`
     ON (`ClroomsMembers`.`clroomID` = `Clrooms`.`ID` AND `ClroomsMembers`.`status`='Active')
    LEFT JOIN `ClroomsGallery`
     ON (`Clrooms`.`thumb` = `ClroomsGallery`.`ID`)
    {$sQueryWhere}
    GROUP BY `Clrooms`.`ID`
    ORDER BY `{$sSortby}` {$sortOrder}, `Clrooms`.`ID` DESC
    LIMIT {$iSqlFrom}, {$iPerPage}
    ";

 


The top line that has [20] was [0] and when I changed it to any number besides  [0] the page resolves with no error and she quits trying to divide by zero.  So now, we know where the zero is coming from, but this number should be pulled from the DB somewhere, which raises the question again:

Where does Groups pull this number from in inc/classes/BxDolGroups.php

Anyone have the answer?  It would be great to know it.

Now, with the 20 in there, it reports that there are NO groups when I know for a fact that there is one group.  Hmmm.... The things we break when at first we try to create.

Quote · 17 Apr 2010

If it was 0, then why was it getting past the if statement?



UGGG. I am currently on my laptop. The stinking thing is so slow and keeps crashing. :( I need a new lappy. Guess i need to go back to my desk to get anything done.


https://www.deanbassett.com
Quote · 17 Apr 2010

Why are you people up at this ridiculous hour?

My opinions expressed on this site, in no way represent those of Boonex or Boonex employees.
Quote · 17 Apr 2010

I fell asleep early. To much time coding.

https://www.deanbassett.com
Quote · 17 Apr 2010

 

If it was 0, then why was it getting past the if statement?



UGGG. I am currently on my laptop. The stinking thing is so slow and keeps crashing. :( I need a new lappy. Guess i need to go back to my desk to get anything done.

 

 When we have NO Class Rooms then it resolves fine.  It's when we have 1 or more Class Rooms that the thing errors out with the division by zero when it's not friggin' zero.  This is the part that makes no sense and why I'm wondering where Groups call the info from for it's DB when ours is erring out on this same exact code.

Quote · 18 Apr 2010

Finally figured this out, along with something else.  It's not a Division by Zero, rather it's that the standards are set in templates/base/BxBaseConfig.php.  Doing some looking, digging and things like that I was able to isolate it out as it was right in front of me the whole time, just wasn't seeing it.  So now we just about have this figured out and will get ready now to replicate it one more time, then write it up.  Should be fun.

Here's where the issue was.  We needed to add some lines to BxBaseConfig.php as follows:

 

Find:

 

var $paginationDifference     = 2; //look design.inc.php, function genPagination

 

Just above it place:

 

// Class Rooms
 var $iClroomsMembersPreNum     = 21; //number of random members shown in main page of Class Rooms
 var $iClroomsMembersResPerPage    = 14;
 
 var $iClroomsSearchResPerPage    = 10;
 var $iClroomsSearchResults_dbnum   = 1;

 

Then go and find:

 

editor_selector : "group_edit_html|guestbookTextArea|story_edit_area|comment_textarea|classfiedsTextArea|blogText",

 

And change it to:

 

editor_selector : "clroom_edit_html|group_edit_html|guestbookTextArea|story_edit_area|comment_textarea|classfiedsTextArea|blogText",

 

(**The 2nd item makes the Tinymce work in the Classrooms create/join pages)

 

This was a lot of time to find and correct, but it finishes up this module on it's base integration and now makes it so we can begin to tailor it even better towards being a true classroom. 

Thanks for all your help on this Deano, in the end it was a null value because it had nothing it could pull from the configs file like it should of been and when this went in the problem was resolved.  Thanks a million for your help.

 

 

Quote · 19 Apr 2010
 
 
Below is the legacy version of the Boonex site, maintained for Dolphin.Pro 7.x support.
The new Dolphin solution is powered by UNA Community Management System.