基本搜索引擎无法正常工作

时间:2012-12-28 16:46:16

标签: php

你好我设法将一个简单的搜索引擎作为基础,所以我开始研究它... 但我得到禁止访问,不知道为什么有人可以帮助我? 用一个表格我给$找到我搜索的单词,它应该搜索它.. thx你的时间!`

    <?php

    if ($searching =="yes")  
    {   
    echo "Results";

    if ($find == "")   
    {   
    echo "You forgot to enter a search term";  
     exit;   
    }   
    mysql_connect("localhost","Anton","zouzou13") or die(mysql_error());  mysql_select_db("Ptyxiakh") or  die(mysql_error());
    $find = strtoupper($find);

    $find = strip_tags($find);

    $find = trim ($find);

    $data =mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");  
     while($result = mysql_fetch_array( $data ))   
    {   
    echo $result['document_name'];   
    echo " ";
    echo $result['first_paragraph'];
    echo "<br>";     
    } 
     $anymatches=mysql_num_rows($data);
    if ($anymatches == 0)   
    {   
    echo "Sorry, but we can not find an entry to match your query<br><br>";   }
     echo "<b>Searched For:</b> " .$find;   }  ?>

ok i made it simpler to see if it works like this:

    <?php
    error_reporting(E_ALL);

    mysql_connect("localhost","Anton","zouzou13") or die(mysql_error()); 
    mysql_select_db("Ptyxiakh") or die(mysql_error()); 


     //Now we search for our search term, in the field the user specified 
     $data = mysql_query("SELECT * FROM documents WHERE keywords LIKE 'helmets'");

      while($result = mysql_fetch_array( $data ))   
    {   
    echo $result['document_name'];   
    echo " ";
    echo $result['first_paragraph'];
    echo "<br>";     
    }

     $anymatches=mysql_num_rows($data); 
     if ($anymatches == 0) 
     { 
     echo "Sorry, but we can not find an entry to match your query<br><br>"; 
     }
    ?> 
But i get the "Sorry, but we can not find an entry to match your query<br><br>"; so i cant connect to database?its just wont work :(



With PDO it should be like this??:

<?php
 //This is only displayed if they have submitted the form 
 if ($searching =="yes") 
 { 
 echo "<h2>Results</h2><p>"; 

 //If they did not enter a search term we give them an error 
 if ($find == "") 
 { 
 echo "<p>You forgot to enter a search term"; 
 exit; 
 } 
 $pdo = new PDO('mysql:host=localhost;dbname=ptyxiakh', 'Anton', 'zouzou13');
 // Otherwise we connect to our Database 
//mysql_connect("localhost","Anton","zouzou13") or die(mysql_error()); 
//mysql_select_db("Ptyxiakh") or die(mysql_error()); 

 // We preform a bit of filtering 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim ($find); 

 //Now we search for our search term, in the field the user specified 
 //$data = mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'"); 
 $data= $pdo->query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");

 //And we display the results 
 while($row = $statement->fetch(PDO::FETCH_ASSOC)) 
 { 
 echo htmlentities($row['document_name']); 
 echo " "; 
 echo htmlentities($row['first_paragraph']);
 echo "<br>";   
 } 

 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 

 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 
?> 

1 个答案:

答案 0 :(得分:0)

我使它的工作非常基础,我猜它已经弃用了东西,但我上传的ppl搜索东西...... 只需制作一个简单的表格然后制作一个php文件

    <?php
 error_reporting(E_ALL);
 //This is only displayed if they have submitted the form 
 echo "<h2>Results</h2><p>"; 
 $find =$_POST["find"];
 //If they did not enter a search term we give them an error 
 if ($find == "") 
 { 
 echo "<p>You forgot to enter a search term"; 
 exit; 
 } 
 // Otherwise we connect to our Database 
 mysql_connect("localhost","Anton","zouzou13") or die(mysql_error()); 
 mysql_select_db("Ptyxiakh") or die(mysql_error()); 

 // We preform a bit of filtering 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim ($find); 

 //Now we search for our search term, in the field the user specified 
 $data = mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'"); 

 //And we display the results 
  while($result = mysql_fetch_array( $data ))   
    {
    echo '<p> <strong>',$result['document_name'], '</strong> <br> ', $result['first_paragraph'],'... <br> </p>';
    }

 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 

 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 

?> 

这对我有用:) thx!