Jump to content

Recommended Posts

Hi

I'm a total newbie with this, but I'm trying to make a serch engine for my mp3 collection.

I found myself a code and tried it out. It's almost working too. :)

But I'm getting a message saying:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /volume1/Web/tmp/search.php on line 48

Even though it's working pretty good, I don't know why this message means.

So, if anyone would like to take a closer look at the code, here it is:

 

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />
<title>Rogers Personal MP3 Search!</title>
</head>
<body>
<h1 align="center">Rogers MP3 Search Engine with PHP/MYSQL</h1>
<p align="center">
<a href="http://www.bjorbekk.net/mp3/search/" ><b>New Search</b></a>
<p></p>
</form>
</body>
</html>


<?PHP
function getmicrotime()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
//initializing connection to the database 
$connection_string = dirname(__FILE__) . "/connectionstring.php";

require_once($connection_string);

//selecting table
mysql_select_db("mp3") or die ( 'Unable to select database.' );

//max number of results on the page
$RESULTS_LIMIT=10;

if(isset($_GET['search_term']) && isset($_GET['search_button']))
{
$search_term = $_GET['search_term'];

    if(!isset($first_pos))
    {
        $first_pos = "0";
    }
     
    $start_search = getmicrotime();
//initializing MySQL Quary  
    $sql_query = mysql_query("SELECT * FROM mp3data WHERE MATCH(Artist,Title,FileName) AGAINST('$search_term')");
   
    //additional check. Insurance method to re-search the database again in case of too many matches (too many matches cause returning of 0 results)
    if($results = mysql_num_rows($sql_query) != 0)
	{
	    $sql =  "SELECT * FROM mp3data WHERE MATCH(Artist,Title,FileName) AGAINST('$search_term') LIMIT $first_pos, $RESULTS_LIMIT";
		$sql_result_query = mysql_query($sql);   	
	}
    else
	{
		$sql = "SELECT * FROM mp3data WHERE (Artist LIKE '%".mysql_real_escape_string($search_term)."%' OR FileName LIKE '%".$search_term."%') ";
		$sql_query = mysql_query($sql);
		$results = mysql_num_rows($sql_query);
		$sql_result_query = mysql_query("SELECT * FROM mp3data WHERE (Artist LIKE '%".$search_term."%' OR FileName LIKE '%".$search_term."%') LIMIT $first_pos, $RESULTS_LIMIT ");
	}
   
    $stop_search = getmicrotime();
//calculating the search time
    $time_search = ($stop_search - $start_search);
}
?>

<?PHP
if($results != 0)
{
?>   
   <!-- Displaying of the results -->
<table border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td width="47%">Results for: <?PHP echo "<i><b><font color=#0000FF>".$search_term."</font></b></i> "; ?></td>
    <td width="53%" align="right" height="22">Results <b>
<?PHP echo ($first_pos+1)." - ";
if(($RESULTS_LIMIT + $first_pos) < $results) echo ($RESULTS_LIMIT + $first_pos);
else echo $results ; ?>
    </b>out of <b><?PHP echo $results; ?>
    </b>for(<b><?PHP echo sprintf("%01.2f", $time_search); ?>
    </b>)seconds </td>
  </tr>
  <tr>
    <form action="" method="GET">
      <td colspan="2" align="center"> <input name="search_term" type="text" value="<?PHP echo $search_term; ?>" size="40">
        <input name="search_button" type="submit" value="Search"> </td>
    </form>
  </tr>
  <?PHP   
    while($row = mysql_fetch_array($sql_result_query))
    {
    ?>
      <tr align="left">
        <td colspan="2"><?PHP echo $row['Artist']; ?>

     <tr align="left">
       <td colspan="2"><?PHP echo $row['Title']; ?>

      <tr align="left">
       <td colspan="2"><?PHP echo $row['FileName']; ?>

  <?PHP
    }
    ?>
</table>
<?PHP
}
//if nothing is found then displays a form and a message that there are nor results for the specified term
elseif($sql_query)
{

?>
<table border="0" cellspacing="2" cellpadding="0">
    <tr>
        <td align="center">No results for<?PHP echo "<i><b><font color=#000000>".$search_term."</font></b></i> "; ?></td>
    </tr>
    <tr>
        <form action="" method="GET">
        <td colspan="2" align="center">
            <input name="search_term" type="text" value="<?PHP echo $search_term; ?>">
            <input name="search_button" type="submit" value="Search">
        </td>
        </form>
    </tr>
</table>
<?PHP
}
?>
<table width="300" border="0" cellspacing="0" cellpadding="0">
<?php
if (!isset($_GET['search_term'])) { ?>
    <tr>
        <form action="" method="GET">
        <td colspan="2" align="center">
            <input name="search_term" type="text" value="<?PHP echo $search_term; ?>">
            <input name="search_button" type="submit" value="Search">
        </td>
        </form>
    </tr>
    <?php
}
?>
  <tr>
    <td align="center">
<?PHP

//displaying the number of pages where the results are sittuated

if($first_pos > 0)
{
  $back=$first_pos-$RESULTS_LIMIT;
  if($back < 0)
  {
    $back = 0;
  }
  echo "<a href='search.php?search_term=".stripslashes($search_term)."&first_pos=$back&search_button=search'></a>";
}

if($results>$RESULTS_LIMIT)
{
  $sites=intval($results/$RESULTS_LIMIT);
  if($results%$RESULTS_LIMIT)
  {
    $sites++;
  }
}
for ($i=1;$i<=$sites;$i++)
{
  $fwd=($i-1)*$RESULTS_LIMIT;
  if($fwd == $first_pos)
  {
      echo "<a href='search.php?search_term=".stripslashes($search_term)."&first_pos=$fwd&search_button=search'><b>$i</b></a> | ";
  }
  else
  {
      echo "<a href='search.php?search_term=".stripslashes($search_term)."&first_pos=$fwd&search_button=search'>$i</a> | ";
  }
}

if(isset($first_pos) && $first_pos < $results-$RESULTS_LIMIT)
{
  $fwd=$first_pos+$RESULTS_LIMIT;
  echo "<a href='search.php?search_term=".stripslashes($search_term)."&first_pos=$fwd&search_button=search'> >></a>";
  $fwd=$results-$RESULTS_LIMIT;
}
?>
    </td>
  </tr>
</table>

Link to comment
https://forums.phpfreaks.com/topic/126195-phpmysql-search-script/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.