2012-01-27 69 views
0

我不知道是否有人可以幫助我試圖將一個實時搜索到我的網站,我已經找到了一個教程和外觀的一面現在正在工作,有人可以告訴我如何編輯後續PHP只從一個表中提取信息。MySQL live Query

表 - 電影 字段呼應對於每個結果 - 電影,描述,圖像

在它被從2個表中提取信息的時刻成功一個顯示的查詢的另一個用於類別分頻器提供的信息的內容,我需要的是刪除類別方面並從單個表中提取信息。

道歉我的PHP知識非常有限,希望能夠最好地描述這個問題。

<p id="searchresults"> 
<?php 
// PHP5 Implementation - uses MySQLi. 
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase'); 
$db = new mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase'); 

if(!$db) { 
    // Show error if we cannot connect. 
    echo 'ERROR: Could not connect to the database.'; 
} else { 

    // Is there a posted query string? 
    if(isset($_POST['queryString'])) { 
     $queryString = $db->real_escape_string($_POST['queryString']); 

     // Is the string length greater than 0? 
     if(strlen($queryString) >0) { 
      $query = $db->query("SELECT * FROM search s INNER JOIN categories c ON s.cat_id = c.cid WHERE name LIKE '%" . $queryString . "%' ORDER BY cat_id LIMIT 8"); 

      if($query) { 
       // While there are results loop through them - fetching an Object. 

       // Store the category id 
       $catid = 0; 
       while ($result = $query ->fetch_object()) { 
        if($result->cat_id != $catid) { // check if the category changed 
         echo '<span class="category">'.$result->cat_name.'</span>'; 
         $catid = $result->cat_id; 
        } 
        echo '<a href="'.$result->url.'">'; 
        echo '<img src="search_images/'.$result->img.'" alt="" />'; 

        $name = $result->name; 
        if(strlen($name) > 35) { 
         $name = substr($name, 0, 35) . "..."; 
        }      
        echo '<span class="searchheading">'.$name.'</span>'; 

        $description = $result->desc; 
        if(strlen($description) > 80) { 
         $description = substr($description, 0, 80) . "..."; 
        } 

        echo '<span>'.$description.'</span></a>'; 
       } 
       echo '<span class="seperator"><a href="http://www.marcofolio.net/sitemap.html" title="Sitemap">Nothing interesting here? Try the sitemap.</a></span><br class="break" />'; 
      } else { 
       echo 'ERROR: There was a problem with the query.'; 
      } 
     } else { 
      // Dont do anything. 
     } // There is a queryString. 
    } else { 
     echo 'There should be no direct access to this script!'; 
    } 
} 
?> 
</p> 

回答

0

更換

SELECT * FROM search s INNER JOIN categories c ON s.cat_id = c.cid WHERE name LIKE '%" . $queryString . "%' ORDER BY cat_id LIMIT 8 

SELECT * FROM movies WHERE movie LIKE '%" . $queryString . "%' ORDER BY movie LIMIT 8 

將是一個良好的開端。

你能從那裏繼續嗎?

另外 - 您在上面的描述中爲字段名稱寫了'Movie' - 我使用了小寫'M',因此您可能需要更改它。

[編輯]在回覆評論1:

讓我們先從簡單 - 與

if ($query) { 
    while ($result = $query ->fetch_object()) { // this line loops through all the results 
     echo '<img src="search_images/'.$result->image.' />'; // check capital I on 'image' 
     echo '<strong>'.$result->movie.'</strong>'; 
     echo $result->description.'<br />'; 
    } 
} 

基本上取代if ($query) { ... },你只要把$result->myFieldName while循環的內部來獲取數據了你想。 希望這應該是足夠讓你去=]

+0

嘿感謝您的回覆,下面的代碼是混淆我作爲其參考其他表我不確定要刪除? – Craig 2012-01-27 22:08:05

+0

請參閱原文答案中的修改。 – 2012-01-28 00:09:37

0

我想:

<p id="searchresults"> 
<?php 
// PHP5 Implementation - uses MySQLi. 
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase'); 
$db = new mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase'); 

if(!$db) { 
    // Show error if we cannot connect. 
    echo 'ERROR: Could not connect to the database.'; 
} else { 

    // Is there a posted query string? 
    if(isset($_POST['queryString'])) { 
     $queryString = $db->real_escape_string($_POST['queryString']); 

     // Is the string length greater than 0? 
     if(strlen($queryString) >0) { 
      $query = $db->query("SELECT * FROM movies WHERE Movie LIKE '%" . $queryString . "%' ORDER BY Movie LIMIT 8"); 

      if($query) { 
       // While there are results loop through them - fetching an Object. 


       while ($result = $query ->fetch_object()) { 
        echo '<a href="'.$result->url.'">'; 
        echo '<img src="search_images/'.$result->img.'" alt="" />'; 

        $name = $result->movie; 
        if(strlen($name) > 35) { 
         $name = substr($name, 0, 35) . "..."; 
        }      
        echo '<span class="searchheading">'.$name.'</span>'; 

        $description = $result->description; 
        if(strlen($description) > 80) { 
         $description = substr($description, 0, 80) . "..."; 
        } 

        echo '<span>'.$description.'</span></a>'; 
       } 
       echo '<span class="seperator"><a href="http://www.marcofolio.net/sitemap.html" title="Sitemap">Nothing interesting here? Try the sitemap.</a></span><br class="break" />'; 
      } else { 
       echo 'ERROR: There was a problem with the query.'; 
      } 
     } else { 
      // Dont do anything. 
     } // There is a queryString. 
    } else { 
     echo 'There should be no direct access to this script!'; 
    } 
} 
?> 
</p>