Jump to content

search script | need to query the database and tell me if there is no matches


dazzclub

Recommended Posts

Hi guys and girls,

 

I am having trouble finding out wether the query made no matches rather than just displaying the matches.

 

So what I would want is for the search term to look at the database, and if there's not matches then say, "No match for your search term"

 

function search_applications() {

global $dbc;

echo '<form method="post" action="" id="search">'. "\n";
echo '<label for="search_applications">Search for an application</label>'. "\n";
echo '<input type="text" name="search_applications" />'. "\n";
echo '<input type="submit" name="submit" />'. "\n";
echo '</form>'. "\n";


if(isset($_POST['submit'])){ 

$search_applications = $_POST['search_applications'];


if ($search_applications == "") 
{ 
echo "<p>You forgot to enter a search term";  
} 

//if (preg_match("/\b$search_applications\b/i")){ 
if(preg_match("/^[  a-zA-Z]+/", $search_applications)){ 


//for testing the variables
//echo $search_applications;

$sql="SELECT title, category, content FROM distributors_content WHERE  title LIKE '%" . $search_applications . "%' OR category LIKE '%" . $search_applications  ."%' OR content LIKE '%" . $search_applications  ."%'";
$result=mysqli_query($dbc, $sql); 
//-create  while loop and loop through result set 
while($row=mysqli_fetch_array($result)){ 
          $title  =$row['title']; 
       $category=$row['category']; 
       $content=$row['content']; 
       
//-display the result of the array 
echo "<ul>\n"; 
echo '<li> <a href="index.php?article='.$row['title'].'&&page=application" title=""> '.$title . ' ' . $category .  '</a></li>'."\n"; 
echo "</ul>";
}
}else{

echo "<p>Enter another search term</p>";
}
} 	 
} 

 

Any pointers would be good :)

 

Thank you

Link to comment
Share on other sites

Thank you so much, it worked..

 

Here's the full code if anyone finds it usefull...

 

function search_applications() {

global $dbc;

echo '<form method="post" action="" id="search">'. "\n";
echo '<label for="search_applications">Search for an application</label>'. "\n";
echo '<input type="text" name="search_applications" />'. "\n";
echo '<input type="submit" name="submit" />'. "\n";
echo '</form>'. "\n";


if(isset($_POST['submit'])){ 

$search_applications = $_POST['search_applications'];


if ($search_applications == "") 
{ 
echo "<p>You forgot to enter a search term";  
} 

//if (preg_match("/\b$search_applications\b/i")){ 
if(preg_match("/^[  a-zA-Z]+/", $search_applications)){ 


//for testing the variables
//echo $search_applications;

$sql="SELECT title, category, content FROM distributors_content WHERE  title LIKE '%" . $search_applications . "%' OR category LIKE '%" . $search_applications  ."%' OR content LIKE '%" . $search_applications  ."%'";
$result=mysqli_query($dbc, $sql); 

if(mysqli_num_rows($result) == 0){
echo "There were no matches!";
}	 

//-create  while loop and loop through result set 
while($row=mysqli_fetch_array($result)){ 
          $title  =$row['title']; 
       $category=$row['category']; 
       $content=$row['content']; 
       
//-display the result of the array 
echo "<ul>\n"; 
echo '<li> <a href="index.php?article='.$row['title'].'&&page=application" title=""> '.$title . ' ' . $category .  '</a></li>'."\n"; 
echo "</ul>";
}
}
} 
}

 

Thanks again

Link to comment
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.