Page 1 of 1

Division by zero error

Posted: Fri Apr 04, 2008 7:10 pm
by xenoalien
When I query a database:

Code: Select all

 
$query  = "SELECT * FROM mytable WHERE keywords LIKE "%$searchedterm%" LIMIT $offset, $rowsPerPage";
 
$result = mysql_query($query) or die('Error, query failed 1' . mysql_error());
I get these errors...

Warning: Division by zero in /___/___/___/___/___/search.php on line 41

Error, query failed Query was empty


Any ideas on what I could be doing wrong? Is the syntax wrong?

Re: Division by zero error

Posted: Fri Apr 04, 2008 7:20 pm
by Christopher
What are the values of $offset, $rowsPerPage ?

Re: Division by zero error

Posted: Fri Apr 04, 2008 7:22 pm
by xenoalien
arborint wrote:What are the values of $offset, $rowsPerPage ?

Code: Select all

$offset = ($pageNum - 1) * $rowsPerPage;
$rowsPerPage = 10;
I tried setting them to 0, 10 and got the same error.

Re: Division by zero error

Posted: Fri Apr 04, 2008 7:32 pm
by Christopher
What's on line 41 in search.php ?

Re: Division by zero error

Posted: Fri Apr 04, 2008 7:33 pm
by xenoalien
arborint wrote:What's on line 41 in search.php ?

Code: Select all

$query  = "SELECT * FROM wallpapertable WHERE keywords LIKE "%$searchedterm%" LIMIT $offset, $rowsPerPage";

Re: Division by zero error

Posted: Fri Apr 04, 2008 7:47 pm
by xenoalien
Ill just give you the entire code lol. hopefully it doesn't have any sensitive info. I put the database info in a different php file.
And the $searchedterm = $_GET['q']; is getting "q" from a input form name.
BTW the code worked when I didn't try to use it as a search.
I had the query just look for all the results and limit them.

Code: Select all

<html>
<head>
<title>Implementing Paging with next and prev</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<center>
<?php
//connect to database
include('config/db_config.php');
  $connection = @mysql_connect($db_host, $db_user, $db_password);
     if(!$connection){ die ("Could not connect to the database: <br />". mysql_error());}
 
$searchedterm = $_GET['q'];
 
if (!$searchedterm) die('No search entered.');
 
  $db_select = @mysql_select_db($db_name);
 
// how many rows to show per page
$rowsPerPage = 10;
 
// by default we show first page
$pageNum = 1;
 
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}
 
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
 
 
error_reporting(E_ALL);
 
ini_set('display_errors', true);
 
$query  = "SELECT * FROM wallpapertable WHERE Keywords LIKE "%$searchedterm%" LIMIT $offset, $rowsPerPage";
echo '<br>' . $query . '<br>';
echo '<br>' . $searchedterm . '<br><br>';
//SELECT * FROM jobcodes WHERE jobname LIKE "%Navigation%" LIMIT 0,1;
$result = mysql_query($query) or die('Error, query failed 1' . mysql_error());
echo '<br><br>the second<br><br>';
// print the random numbers
while($row = mysql_fetch_array($result))  //while(list($val) = mysql_fetch_array($result))
{
        $walltype = $row['Type'];
        switch ($walltype)
        {
            case 1:
            $WallType2 = '3d';
            break;
            case 2:
            $WallType2 = 'abstract';
            break;
            case 3:
            $WallType2 = 'animals';
            break;
            case 4:
            $WallType2 = 'anime';
            break;
            case 5:
            $WallType2 = 'cars';
            break;
            case 6:
            $WallType2 = 'cartoons';
            break;
            case 7:
            $WallType2 = 'computer';
            break;
            case 8:
            $WallType2 = 'game';
            break;
            case 9:
            $WallType2 = 'holiday';
            break;
            case 10:
            $WallType2 = 'landscape';
            break;
            case 11:
            $WallType2 = 'military';
            break;
            case 12:
            $WallType2 = 'movies';
            break;
            case 13:
            $WallType2 = 'motorcycles';
            break;
            case 14:
            $WallType2 = 'music';
            break;
            case 15:
            $WallType2 = 'nature';
            break;
            case 16:
            $WallType2 = 'ocean';
            break;
            case 17:
            $WallType2 = 'people';
            break;
            case 18:
            $WallType2 = 'photography';
            break;
            case 19:
            $WallType2 = 'places';
            break;
            case 20:
            $WallType2 = 'religious';
            break;
            case 21:
            $WallType2 = 'scifi';
            break;
            case 22:
            $WallType2 = 'space';
            break;
            case 23:
            $WallType2 = 'sports';
            break;
            case 24:
            $WallType2 = 'misc';
            break;
  
            default:
            $WallType2 = '0';
            break;
        }
    //echo $row['Name'] . ' | ' . $row['Keywords'] . '<br>';
    /*echo '<br> <a href="' . '/wallpaper/' . $WallType2 . '/' . $row['Name']. '.jpg' .
        '"> <img src='. '/wallpaper/' . $WallType2
         . '/thumb/' . $row['Name'] . 'tn.jpg' . '> </a><br><br><br>'; */
         //<a href="http://www.espn.com" target="_blank> <img src="ahman.gif"> </a>
         $pathofwall = 'http://www.wallpapersearch.net/wallpaper/' . $WallType2 . '/' . $row['Name']. '.jpg';
         $size = GetImageSize($pathofwall);
         $width = $size[0];
         $height = $size[1];
 
        echo '<table width="780" height="131" border="0">';
         echo '<tr>
          <td width="177">' . 
          '<br> <a href="' . '/wallpaper/' . $WallType2 . '/' . $row['Name']. '.jpg' .
        '"> <img src='. '/wallpaper/' . $WallType2
         . '/thumb/' . $row['Name'] . 'tn.jpg' . '> </a><br><br><br>' .'</td>
           <td width="593">' . '<b>Keywords:</b> ' . $row['Keywords'] . '<br><br><b>Resolution:</b> '
            . $width . ' x ' . $height . '</td>
           </tr>
           </table>';
}
 
echo '<br>';
 
// how many rows we have in database
$query   = "SELECT COUNT(*) AS numrows FROM wallpapertable";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
 
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
 
$self = $_SERVER['PHP_SELF'];
 
// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link
 
// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
    $page = $pageNum - 1;
    $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
    
    $first = " <a href=\"$self?page=1\">[First Page]</a> ";
} 
else
{
    $prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
    $first = ' [First Page] '; // nor 'first page' link
}
 
// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
    $page = $pageNum + 1;
    $next = " <a href=\"$self?page=$page\">[Next]</a> ";
    
    $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
} 
else
{
    $next = ' [Next] ';      // we're on the last page, don't enable 'next' link
    $last = ' [Last Page] '; // nor 'last page' link
}
 
// print the page navigation link
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
 
include '../library/closedb.php';
?>
</center>
</body>
</html>

Re: Division by zero error

Posted: Fri Apr 04, 2008 8:08 pm
by EverLearning
Try putting single quotes around the variable instead of double quotes in a double quoted string. :)

Code: Select all

$query  = "SELECT * FROM wallpapertable WHERE keywords LIKE "%$searchedterm%" LIMIT $offset, $rowsPerPage";
:arrow:

Code: Select all

$query  = "SELECT * FROM wallpapertable WHERE keywords LIKE '%$searchedterm%' LIMIT $offset, $rowsPerPage";
And use [ code=php ] tags.