hi all im attempting to update coryApp script and im getting the following error

PHP Warning: Division by zero

can anyone help on this please be much appreicated heres is where the error is coming from

if(isset($error) && !empty($error))
            echo '<p style="margin:0px; padding:5px 20px"><font color="#FF0000"><small><i>'.$error.'</i></small></font></p>';
        $config['showeachside'] = 4;
        $config['per_page'] = 20;
        $config['js_numrows_page'] = mysqli_num_rows(getListState($str, ''));
        $config['curpage'] = empty($_GET['p'])?1:$_GET['p'];
        $config['rs_start'] = ($config['curpage']*$config['per_page'])-$config['per_page'];
        if($config['js_numrows_page'] < $config['per_page'])
            $config['per_page'] = $config['js_numrows_page'];
        $page = (isset($_GET['p']) && intval($_GET['p'])>0)?'&p='.$_GET['p']:'';
        $config['cururl'] = $base_url.'admincp/states.php'.$pstr;
        $rs_maxpage = ceil($config['js_numrows_page']/$config['per_page']);
        $paging = Pagination($config);
        $list = getListState($str, " limit ".$config['rs_start'].", ".$config['per_page']);
        if(mysqli_num_rows($list)>0){

and here is the function for it

if(!function_exists('getListState')){
    function getListState($str='', $limit=''){
        global $conn;
        $sql = "SELECT StateID as Id, State as L1Value, s.CountryID as L3Value, Country as L4Value from ".$GLOBALS['table_prefix']."states as s inner join ".$GLOBALS['table_prefix']."countries as c on c.CountryID = s.CountryID ".$str." order by StateID desc ".$limit;
        $query = mysqli_query($conn,$sql);
        if(!$query)
            return '';
        else return $query;
        }
    }

Dani AI

Generated

Short diagnosis: the division-by-zero comes from computing the number of pages when the per-page value has been set to 0. As pointed out, that happens when the resultset is empty (or the query fails) and the code reduces the per-page count to the total row count. If the total is zero, per-page becomes zero and the call to compute pages triggers the PHP warning.

Safer approach (do three things): read the query result into a variable and derive a numeric total only when the result is valid; only shrink per_page when the total is > 0; and guarantee per_page is at least 1 before any division. Example safe snippet:

$res = getListState($str, '');
$total = ($res && $res instanceof mysqli_result) ? (int) $res->num_rows : 0;

if ($total > 0 && $total < $config['per_page']) {
    $config['per_page'] = $total;
}
$config['per_page'] = max(1, (int) $config['per_page']);
$rs_maxpage = $config['per_page'] ? (int) ceil($total / $config['per_page']) : 0;

Additional notes and cautions: change getListState so it returns false on query failure (instead of an empty string) and log mysqli_error($conn) when queries fail — this makes the checks above reliable. Replacing a row-check like > 0 with > 100 (as in an earlier reply) does not address the root cause and will not prevent the division-by-zero. If $str or the limit clause come from user input, sanitize them or use prepared statements to avoid SQL injection. For quick debugging, log the values of $total, $config['per_page'], and the result of getListState to confirm the control flow reported by .

Recommended Answers

All 3 Replies

anytime your getListState returns an empty set $config['per_page'] will be 0

okies would i have to change this

from

if(mysqli_num_rows($list)>0){

to

if(mysqli_num_rows($list)>100){

as i have read in google but still brings same error

output all your variables at different points to see whats in them

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.