Jump to content


This topic is now archived and is closed to further replies.


PHP + MySQL Search Problems

Recommended Posts


the following query: [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]"SELECT * FROM tutorials WHERE activation = '1' AND catid = '$catid' AND name LIKE '%$search%' LIMIT $start, $limit";[/quote] is going wrong. It shows tutorials where the $search has nothing in common with the name.

The form is:

[code]<form action="search.php">Search:&nbsp;&nbsp;&nbsp;
<form method="post" action="search.php">
  <input type="text" name="search" size=25 maxlength=25>    
<select name="category">
                <option value="Default">Choose a category</option>
        <?php $query = "SELECT * FROM category";
    $result = mysql_query ($query);
    while ($row = mysql_fetch_array
        ($result, MYSQL_NUM)) {
echo "<option value='{$row[0]}'>{$row[1]}</option>";} ?>

  <input name="image" type="image" src="images/search.jpg" alt="0"/>

and the search.php is:

$num = $_GET['num'];//Get the numeration of the page
if(empty($num)){//if the numeration is empty
$num = 1;//the numeration is 1
$limit = 15;//Sets the limit of results to display in each page, change if you want.
The query will start selecting the numeration, for example 2, less 1, that would be 1
* the limits of results to show per page, 2 in this case so it would be 1*2 = 2, it will
start from 2;) if the limit would be 5 and the numeration would be 3 if would be (3-1)*5 = 10
$start = ($num-1)*$limit;
$start = round($start,0);//rounds the result
This query will select the contrene FROM the start and with a limit of 2, in this case,
because the variable $limit is 2
You can add a WHERE something= '$something' for example, or delete the ORDER by `id`, or change it,

$catid = $_POST['category'];
$query = "SELECT * FROM tutorials WHERE activation = '1' AND catid = '$catid' AND name LIKE '%$search%' LIMIT $start, $limit";
$result = mysql_query($query);

while ($rowss = mysql_fetch_array($result)){
         $author = $rowss['author'];
    $description = $rowss['description'];
           $name = $rowss['name'];
       $imageurl = $rowss['imageurl'];
            $url = $rowss['url'];
              $url2 = 'viewtut.php?id='.$rowss['id'].'';
    $totalviewed = $rowss['hits'];
           $cat1 = $rowss['catid'];
     $tut_rating = $rowss['rating'];
     $sub = $rowss['subcatid'];
           $sql3 = "SELECT * FROM `category` WHERE `cat_id` = '$cat1'";
           $cat3 = $rows3['category'];
  echo "
    <div class='tutorial'>
      <div class='tutorialtitle'> <span class='style1'><a href='".$url2."'>".$name."</a></span><br />
        Author: <a href='#'>".$author."</a> | Rating: <span class='style3'>".tut_stars($tut_rating)."<br></span></div>
      <img src='".$imageurl."' width='44' height='43' alt='' /><br />".$description."<br />
      <span class='style2'>22nd March 2006 <span class='style3'><a href='#'>Report Tutorial</a></span> <span class='style3'>Total Views: <strong>".$totalviewed."</strong></span> <span class='style3'>Categorgy: <a href='viewcat.php?act=maincat&id=".$catids."'><strong>".$cat3."</strong></a></span></span></div>
$totalpages = mysql_num_rows(mysql_query("SELECT * from `tutorials` WHERE `subcatid` = '$subcat' and `catid` = '$idf'"));//Get the total number of results
Total resutls/ the limit, in this example is 2, so if there are 10 total result and the limit is 2
there will be 5 pages.
$totalpages = $totalpages / $limit;
$totalpages = round($totalpages,0);
$c = 0;//The variable c is 0
echo "<br>";//make a <br> to separate the results from the [1][2]...
while($c<$totalpages){//while c is < than the total pages
$page = $c + 1;//sets the variable $page as 0 + 1 = 1
if($_GET['num']==$page){//Gets the number of the page and if its the same that the page
echo "[$page] ";//its only echoes the page, not the url to this page
echo "<a href=?act=subcat&id=".$cat1."&sub=".$sub."&num=$page>[$page] </a>";//it echoes the url to the page
$c = $c+1;
echo "<br>".$totalpages." Pages in total.";//echoes the total pages


Does anyone have any explanation(s) why this is happening?


Share this post

Link to post
Share on other sites
What sorts of results is it getting, I may be wrong but I thought %$search% meant anything that contained $search in it. That could mean a lot of things.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.