2017-10-14 141 views
0

警告:mysql_num_rows()預計參數1是資源,布爾在/storage/ssd5/102/3255102/public_html/search.php給出線29PHP警告同時使搜索引擎

我的代碼:

<?php 
    $button = $_GET [ 'submit' ]; 
    $search = $_GET [ 'search' ]; 

    if(!$button) 
     echo "you didn't submit a keyword"; 
    else { 
     if(strlen($search) <= 1) 
       echo "Search term too short"; 
     else { 
       echo "You searched for <b> $search </b> <hr size='1' > </ br > "; 
       mysql_connect("localhost","id3255102_data","I AM NOT GOING TO TELL YOU MY PASS!") ; 
       mysql_select_db("id3255102_base"); 

       $search_exploded = explode (" ", $search); 
       $x = 0; 
       foreach($search_exploded as $search_each) { 
         $x++; 
         $construct = ""; 
         if($x == 1) 
           $construct .="keywords LIKE '%$search_each%'"; 
         else 
           $construct .="AND keywords LIKE '%$search_each%'"; 
       } 

       $construct = " SELECT * FROM SEARCH_ENGINE WHERE $construct "; 
       $run = mysql_query($construct); 

       $foundnum = mysql_num_rows($run); 

       if ($foundnum == 0) 
         echo "Sorry, there are no matching result for <b> $search </b>. </br> </br> 1. Try more general words. for example: If you want to search 'how to create a website' then use general keyword like 'create' 'website' </br> 2. Try different words with similar meaning </br> 3. Please check your spelling"; 
       else { 
         echo "$foundnum results found !<p>"; 

         while($runrows = mysql_fetch_assoc($run)) { 
           $title = $runrows ['title']; 
           $desc = $runrows ['description']; 
           $url = $runrows ['url']; 

           echo "<a href='$url'> <b> $title </b> </a> <br> $desc <br> <a href='$url'> $url </a> <p>"; 

         } 
       } 

     } 
    } 
?> 

我在等這麼久所以請幫助我。

PHP版本:5.6 教程:http://mrbool.com/how-to-create-your-own-search-engine-with-php-and-mysql/32733

+3

回聲你的查詢是否有效 – urfusion

+1

檢查你的查詢結果集中的行數是否成功或失敗時是否爲FALSE。 – honarkhah

+2

您使用的是完全過時的mysql連接器,這個連接器已經過時多年了,並且自從PHP版本7以來實際上已經被完全刪除。結果,您的代碼對sql注入攻擊全面開放。請閱讀將「準備好的語句」與「參數綁定」組合使用以避免此類漏洞的優點。 – arkascha

回答

0

你的問題涉及到$構建= 「」; & $ construct。=「AND關鍵字LIKE'%$ search_each%'」;

你$構建設置爲空環路和您的查詢像這樣運行後:

SELECT * FROM SEARCH_ENGINE WHERE和關鍵字LIKE '%$ search_each%'

<?php 
    $button = $_GET [ 'submit' ]; 
    $search = $_GET [ 'search' ]; 

    if(!$button) 
     echo "you didn't submit a keyword"; 
    else { 
     if(strlen($search) <= 1) 
       echo "Search term too short"; 
     else { 
       echo "You searched for <b> $search </b> <hr size='1' > </ br > "; 
       mysql_connect("localhost","id3255102_data","I AM NOT GOING TO TELL YOU MY PASS!") ; 
       mysql_select_db("id3255102_base"); 

       $search_exploded = explode (" ", $search); 
       $construct = ''; 
       foreach($search_exploded as $search_each) { 
        $construct .="AND keywords LIKE '%$search_each%'"; 
       } 

       $construct = " SELECT * FROM SEARCH_ENGINE WHERE 1 $construct "; 
       $run = mysql_query($construct); 

       $foundnum = mysql_num_rows($run); 

       if ($foundnum == 0) 
         echo "Sorry, there are no matching result for <b> $search </b>. </br> </br> 1. Try more general words. for example: If you want to search 'how to create a website' then use general keyword like 'create' 'website' </br> 2. Try different words with similar meaning </br> 3. Please check your spelling"; 
       else { 
         echo "$foundnum results found !<p>"; 

         while($runrows = mysql_fetch_assoc($run)) { 
           $title = $runrows ['title']; 
           $desc = $runrows ['description']; 
           $url = $runrows ['url']; 

           echo "<a href='$url'> <b> $title </b> </a> <br> $desc <br> <a href='$url'> $url </a> <p>"; 

         } 
       } 

     } 
    } 
?>