Having Trouble Making SQL Search Query

Discussion in 'PHP' started by scottlpool2003, Dec 13, 2007.

  1. #1
    Hi there... Here's where I'm putting it into place:

    http://www.myspace4yourpage.com/search.php

    Here's the coding i've done so far:

    <?php include"includes/dbconnect.php";?>
    <?php
    $name = $_GET['name'];
    $cat = $_GET['cat'];
    ?>
    <table width="236" border="0" cellspacing="0" cellpadding="0">
    <tr><form id="search" method="post" action="search.php">
    <td width="180"><input name="keyword" type="text" id="keyword" size="30" maxlength="50"></td>
    <td width="56"><input type="submit" name="Search" value="Search"></td></form>
    </tr>
    </table>
    <?php
    $dbhost = 'localhost';
    $dbuser = 'nobutyer_scottfu';
    $dbpass = '123456';
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
    
    $dbname = 'nobutyer_layouts';
    mysql_select_db($dbname);
    ?>
    		<?php
    // CYBO'S EDIT:
    $cat = $_GET['cat'];
    // Change this value to the maxiumum columns you want:
    $maxcols = 1;
    
    
    // =======================
    // Show guestbook entries
    // =======================
    
    // how many guestbook entries to show per page
    $rowsPerPage = 1;
    
    // by default we show first page
    $pageNum = 1;
    
    // if $_GET['page'] defined, use the value as page number
    if(isset($_GET['page']))
    {
        $pageNum = $_GET['page'];
    }
    
    // counting the offset ( where to start fetching the entries )
    $offset = ($pageNum - 1) * $rowsPerPage;
    
    // prepare the query string
    $query = "SELECT * FROM videos WHERE name LIKE '%$search%' OR cat LIKE '%$search%'".            
    			"order by videos.id DESC
    
    LIMIT $offset, $rowsPerPage";  // LIMIT is the core of paging
    
    // exeAnimal the query 
    $result = mysql_query($query) or die('Error, query failed. ' . mysql_error());
    
    // if the guestbook is empty show a message
    if(mysql_num_rows($result) == 0)
    {
    ?>
    Whoops! Sorry, we don't seem to have any icons in this category...<br><br>
    Try again later!
    <?php
    }
    else
    {
        echo '<table border="0" cellspacing="0" cellpadding="0" align="center" width="560"><tr>';
        $currentcols = 0;
        // get all guestbook entries
        while($row = mysql_fetch_array($result))
        {
            // list() is a convenient way of assign a list of variables
            // from an array values 
            list($id, $name, $cat, $cat_url, $subcat, $entry_date, $subcat_url, $code, $active, $views, $url, $imgurl) = $row;
            if ($currentcols == 0)
            {
                echo "</tr>\n<tr>\n";
            }
            $currentcols += 1;
    ?><?php
    $hm = "/home2/scottfu/public_html/HSRS";
    $hm2 = "http://www.myspace4yourpage.com/HSRS";
    
    ?>
    
    
    
    <td align="center" valign="top" style="padding:10px;font-size:11px;font-family:arial;color:#0097E6;">
    <b><?=$name;?></b><a href="http://www.myspace4yourpage.com/layout-view.php?cat=<?=$cat_url;?>&name=<?=$url;?>"><br><img src="http://www.myspace4yourpage.com/images/layouts/<?=$imgurl;?>" border="0" width="130" height="90" alt="<?=$subcat;?> - <?=$name;?>" style="border: 1px solid #000000;"></a><br> 
    
    </td>
       <?php
               if ($currentcols == $maxcols)
            {
                $currentcols = 0;
            }
        } // end while
        echo "</tr>\n</table>";
    ?>
    <?php
        } // end while
    
    // below is the code needed to show page numbers
    
    // count how many rows we have in database
    $query   = "SELECT COUNT(id) AS numrows FROM videos WHERE active = 'yes' AND name = '$search'";
    $result  = mysql_query($query) or die('Error, query failed. ' . mysql_error());
    $row     = mysql_fetch_array($result, MYSQL_ASSOC);
    $numrows = $row['numrows'];
    
    // how many pages we have when using paging?
    $maxPage  = ceil($numrows/$rowsPerPage);
    $nextLink = '';
    
    // show the link to more pages ONLY IF there are 
    // more than one page
    if($maxPage > 0)
    {
        // this page's path
        $self     = $_SERVER['PHP_SELF'];
        
        // we save each link in this array
        $nextLink = array();
        
        // create the link to browse from page 1 to page $maxPage
        for($page = 1; $page <= $maxPage; $page++)
        {
            $nextLink[] =  "<a href=\"http://www.myspace4yourpage.com/layouts.php?cat=$cat&page=$page\" title=\"Page $page\" style=\"color:#0097E6\">$page</a>";
        }
        // join all the link using implode() 
        $nextLink = "" . implode(' &raquo; ', $nextLink);
    }
    
    // close the database connection since
    // we no longer need it
    #include 'library/closedb.php';, $nextLink';
    
    ?>
    PHP:
    I could really use with a little help. Basically I want to query the database to display results that have been searched.

    I'm not much good with PHP but I'm trying!

    If you can help me, please do.

    Regards,

    Scott.
     
    scottlpool2003, Dec 13, 2007 IP
  2. daman371

    daman371 Peon

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    well first off in the sql query it should use a like statement.

    
    $query   = "SELECT COUNT(id) AS numrows FROM videos WHERE active = 'yes' WHERE name LIKE '%$search%'";
    
    Code (markup):
     
    daman371, Dec 18, 2007 IP