0

I get this warning > Warning: Division by zero in < if i type something that doesnt exist in database.

If i type something that exist everything works fine, i cant find or solve the problem.

Here below is the script i use at the moment, my php skils are not perfect but i try.

<?php 


$button = mysql_real_escape_string($_GET ['submit']);
$search = $str = $stt = mysql_real_escape_string(strip_tags($_GET ['search']));
$str = strtoupper($str);
$stt = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");

if(strlen($search)<=2) {

echo 'Nothing found for <b>'.strip_tags($str).'</b>. Please try something else ! ';

}else{

echo 'You searched for <b>'.$stt.'</b> ';


echo "";
include 'extern_/connectsearch.php';

$search_exploded = explode (" ", $search);

foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="title LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%'";
   }



$constructs ="SELECT * FROM table WHERE $construct";
$run = mysql_query($constructs);

$foundnum = mysql_num_rows($run);

if ($foundnum==0)

echo 'No results for <b>'.$search.'</b> ';

else
{ 


$per_page = 36;  
$id = (int)mysql_real_escape_string($_GET['page']);
$max_pages = ceil($foundnum / $per_page);
if(!$id)
$id=0; 


$p_num = $per_page*$id;  


if($id) 
    $p_num = ($id - 1) * $per_page;             //first item to display on this page
else
    $p_num = 0;

$getquery = mysql_query("SELECT * FROM `table` WHERE $construct ORDER BY date DESC  LIMIT $p_num, $per_page");




$title = $row ['title'];

$url = $row ['url'];


while($row = mysql_fetch_assoc($getquery))
{




echo '<div class="grid">


<a href="'. $row['url'] .'"  
id="'. $row['id'] .'" 
alt="'. $row['title'] .'" 
title="'. $row['title'] .'" 
target="_blank">

<h3 class="style">'. substr($row['title'], 0, 34).' ...</h3>

</a>



</div>
</div>
</div>
              ';


 }


 }
echo '<center>';


?>

 <!-- pagination -->

<?php

echo '<center>';


    $page = $id;
    $total_pages = $foundnum;
    $limit = $per_page;
    if ($page == 0) $page = 1;                  
    $prev = $page - 1;                          
    $next = $page + 1;                          
    $lastpage = ceil($total_pages/$limit);      
    $lpm1 = $lastpage - 1;  
    $adjacents = 5;     




    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"paginate\">";

        if ($page > 1) 
            $pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$prev\">Prev</a>";



        if ($lastpage < 7 + ($adjacents * 2))   
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<a href='".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter' class='current'>$counter</a>";
                else
                    $pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter\">$counter</a>";                   
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))    
        {

            if($page < 1 + ($adjacents * 2))        
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<a href='".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter' class='current'>$counter</a>";
                    else
                        $pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter\">$counter</a>";                   
                }

            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {

                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<a href='".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter' class='current'>$counter</a>";
                    else
                        $pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter\">$counter</a>";                   
                }

            }

            else
            {

                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<a href='".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter' class='current'>$counter</a>";
                    else
                        $pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$counter\">$counter</a>";                   
                }
            }
        }


        if ($page < $counter - 1) 
            $pagination.= "<a href=\"".$_SERVER['PHP_SELF']."?search=$search&submit=search&page=$next\">Next</a>";

        $pagination.= "</div>\n";       
    }

    echo $pagination;

echo '</center>';
} 

?>

Warning: Division by zero in home/search.php on line 348

line 348

$lastpage = ceil($total_pages/$limit);

And on more thing that i would like to know is if this script safe enough is ?

Any help is appreciated. Thanks in advance

Tasos
  • 37
  • 7
  • Where is line #348? It clearly tells you that you are trying to do something like - `5/0`. So you need to account for when there is a `0` value. In regards to `if this script safe enough` see - http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Sean Oct 05 '14 at 22:52
  • please display pagination only if You found results ;) – ryrysz Oct 05 '14 at 23:05
  • @Sean I do not understand what you mean, could you give me an example ? – Tasos Oct 05 '14 at 23:12
  • The problem is obviously `$limit` equals 0. I'd start with `echo $limit;` and see what you get... – Mark Miller Oct 05 '14 at 23:14
  • @MarkM I already try echo $limit; but also '.$limit.' somewhere in the page, but i get the same error – Tasos Oct 05 '14 at 23:22
  • Example -> `$n=1;$d=0; echo $n/$d; // Gives Warning: Division by zero.` – Sean Oct 05 '14 at 23:29
  • 1
    I think your problem is that `$per_page = 36;` is inside the `else` of `if ($foundnum==0)`. So when you use it in `$limit = $per_page;` then `$per_page;` is undefined, and `$lastpage = ceil($total_pages/$limit);` causes `$limit` to equal `0`, giving you the warning. Try either moving the `$per_page = 36;` to outside the `if/else` or change the `$limit = $per_page;` to `$limit = 36;` – Sean Oct 05 '14 at 23:32
  • @Sean YES !!! you just solve my problem.... litle mistakes can make big problem sometimes... Thank you – Tasos Oct 05 '14 at 23:59
  • A better thought. As @ryrysz points out, only display the pagination navigation *if* results are found. It may be as simple as moving the closing bracket `}` before `echo '
    ` to the end of the file.
    – Sean Oct 06 '14 at 00:01

1 Answers1

1

Your problem is that the code don't set $per_page in all cases. If you check your code you could see that the only place that sets this variable is when:

if ($foundnum==0)

But the code still runs and when executes

$limit = $per_page;
if ($page == 0) $page = 1;                  
$prev = $page - 1;                          
$next = $page + 1;                          
$lastpage = ceil($total_pages/$limit);  

The var $per_limit is not defined and its value is null. So, when you make the division $total_pages/$limit PHP casts null to 0 and that's the reason why you only gets this error when no DB value is returned.

The solution is as simple as set in all the cases the $per_page value.

Miguel
  • 1,361
  • 1
  • 13
  • 24