2016-06-23 51 views
0

因此,我有一個大型數據庫(120k行),我正在構建一個小型的網絡應用程序,以使其對社區(基於天文的東西)有用。我是PHP新手,正在尋找一些幫助。 我有一個search.php中的文件和一個類的search.php文件,此刻的結果是這樣的:使用class-search.php查詢PHP MYSQL搜索結果

2 results found 

Array 
(
    [count] => 2 
    [results] => Array 
     (
      [0] => stdClass Object 
       (
        [disc] => LOC 7 
        [fstdate] => 2016 
        [lstdate] => 2016 
        [lstpa] => 170 
        [lstsep] => 0.4 
        [stype] => 
        [comp] => AB 
        [fstmag] => 13.80 
        [secmag] => 14.10 
        [dnum] => 
        [rahour] => 05 
        [ramin] => 38 
        [rasec] => 48.04 
        [decdeg] => -02 
        [decmin] => 27 
        [decsec] => 14.2 
       ) 

      [1] => stdClass Object 
       (
        [disc] => LOC 7 
        [fstdate] => 2016 
        [lstdate] => 2016 
        [lstpa] => 284 
        [lstsep] => 7.5 
        [stype] => 
        [comp] => AB,C 
        [fstmag] => 13.20 
        [secmag] => 16.60 
        [dnum] => 
        [rahour] => 05 
        [ramin] => 38 
        [rasec] => 48.04 
        [decdeg] => -02 
        [decmin] => 27 
        [decsec] => 14.2 
       ) 

     ) 

) 

我期待制表的結果和我有一個大概的瞭解了我我在做什麼,但我沒有把所有的東西都聯繫起來 - 任何人都可以提出任何建議。 search.php中的關鍵部分如下:(代碼可能不完全準確,因爲我已經採取了一些開關和IFS減少後的長度)

<?php 

//Check if search data was submitted 
if (isset($_GET['s'])) { 
    // Include the search class 
    require_once(dirname(__FILE__) . '/class-search.php'); 

    // Instantiate a new instance of the search class 
    $search = new search(); 

    // Store search term into a variable 
    $search_term = htmlspecialchars($_GET['s'], ENT_QUOTES); 


    // Send the search term to our search class and store the result 
    $search_results = $search->search($search_term); 
} 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
    <title>WDSC search</title> 
    </head> 
    <body> 
    <h1>Search the WDSC</h1> 
    <div class="search-form"> 
     <form action="" method="get"> 
     <div class="form-field"> 
      <label for="search-field">Search term</label> 
      <input type="search" name="s" placeholder="Enter your search term..." results="5" value="<?php echo $search_term; ?>"> 
      <input type="submit" value="Search"> 
      ........ 
     </form> 
    </div> 
    <?php if ($search_results) : ?> 
    <div class="results-count"> 
     <p><?php echo $search_results['count']; ?> results found</p> 
    </div> 
    <div class="results-table"> 
     <?php foreach ($search_results['results'] as $search_result) : ?> 
     <div class="result">   
     <p><?php echo $search_result->title; ?></p> 
     </div> 
     <?php endforeach; ?> 
    </div> 
    <div class="search-raw"> 
     <pre><?php print_r($search_results); ?></pre> 
    </div> 
    <?php endif; ?> 
    </body> 
</html> 

和類搜索的關鍵部分。 PHP是這樣的:

<?php 

/** 
* Performs a search 
* 
* This class is used to perform search functions in a MySQL database 
* 
*/ 
class search { 
    /** 
    * MySQLi connection 
    * @access private 
    * @var object 
    */ 
    private $mysqli; 

    /** 
    * Constructor 
    * 
    * This sets up the class 
    */ 
    public function __construct() { 
    // Connect to our database and store in $mysqli property 
    $this->connect(); 
    } 
    /** 
    * Database connection 
    * 
    * This connects to our database 
    */ 
    private function connect() { 
    $this->mysqli = new mysqli('server', 'user', 'pass', 'db'); 
    } 

    /** 
    * Search routine 
    * 
    * Performs a search 
    * 
    * @param string $search_term The search term 
    * 
    * @return array/boolen $search_results Array of search results or false 
    */ 
    public function search($search_term) { 
    // Sanitize the search term to prevent injection attacks 
    $sanitized = $this->mysqli->real_escape_string($search_term); 
    // Define variable for search category from list selection 
    $cat = $_GET['category']; 


    // Run the query. 
    // Query for discoverer 
    $query = $this->mysqli->query(" 
     SELECT disc, fstdate, lstdate, lstpa, lstsep, stype, comp, fstmag, secmag, dnum, rahour, ramin, rasec, decdeg, decmin, decsec 
     FROM WDS_CAT 
     WHERE $cat LIKE '{$sanitized}%' 

    "); 
    } 


    // Check results 
    if (! $query->num_rows) { 
     return false; 
    } 

    // Loop and fetch objects 
    while($row = $query->fetch_object()) { 
     $rows[] = $row; 
    } 

    // Build our return result 
    $search_results = array(
     'count' => $query->num_rows, 
     'results' => $rows, 
    ); 

    return $search_results; 
    } 
} 
endif; 
?> 

我試圖通過手段

echo '<table border=1px>'; // opening table tag 
echo'<th>Discoverer</th><th>First year observed</th><th>Last year observed</th><th>Last position angle</th><th>Last separation</th><th>Spectral type</th><th>Components</th><th>Primary Magnitude</th><th>Secondary magnitude</th><th>Durchmusterung number</th><th>Right ascension</th><th></th><th></th><th>Declination</th><th></th><th></th>'; //table headers 

while($data = mysql_fetch_array($search_results)) 
{ 
// we are running a while loop to print all the rows in a table 
echo'<tr>'; // printing table row 
echo '<td>'.$data['disc'].'</td><td>'.$data['fstdate'].'</td><td>'.$data['lstdate'].'</td><td>'.$data['lstpa'].'</td><td>'.$data['lstsep'].'</td><td>'.$data['stype'].'</td><td>'.$data['comp'].'</td><td>'.$data['fstmag'].'</td><td>'.$data['secmag'].'</td><td>'.$data['dnum'].'</td><td>'.$data['ragiyr'].'</td><td>'.$data['ramin'].'</td><td>'.$data['rasec'].'</td><td>'.$data['decdeg'].'</td><td>'.$data['decmin'].'</td><td>'.$data['decsec'].'</td>'; // we are looping all data to be printed till last row in the table 
echo'</tr>'; // closing table row 
} 

echo '</table>'; //closing table tag 
?> 

    <?php endif; ?> 

,但沒有喜悅的錶鏈接到變量$ search_results - 任何人都可以提出好的建議請。

在此先感謝

+0

我不明白你的問題是什麼......你是什麼意思「製表結果」? – matiaslauriti

回答

0

假設您只是試圖在HTML表中輸出結果......?

UPDATE:

foreach($search_results['results'] as $resultRow) { 
     var_dump($resultRow); 
    } 

這應該做的伎倆。你已經返回到處理您的陣列中的結果,所以你只需要循環,陣列上,而不是再次調用mysql_fetch_array()


我會先加

var_dump($data); 
exit(); 

右後

while($data = mysql_fetch_array($search_results)) 
{ 

只循環一次,看看每個循環返回的內容。它可能只返回[count]和[results]數組,所以你可能需要在[results]數組上重新循環。查看mysql_fetch_array的PHP參考資料http://php.net/manual/en/function.mysql-fetch-array.php

例如

while($data = mysql_fetch_array($search_results)) 
{ 
if(is_array($data['results'])){ 
    foreach($data['results'] as $resultRow) { 
     var_dump($resultRow); 
    } 
} 

在foreach循環您可以添加HTML輸出,你有上述

注:這一切是基於mysql_fetch_array假設()返回的數據結構,按您的第一個代碼片斷。

2 results found 

Array 
(
    [count] => 2 
    [results] => Array 
     (
      [0] => stdClass Object 
       (
+0

明天我會嘗試你的建議 - 我的確嘗試將結果輸出到HTML表格。謝謝 –

+0

請注意,您將需要某種形式的分頁,因爲顯示的120K記錄將耗盡PHP內存或崩潰瀏覽器...始終限制結果集! :) –

0

固定!!

我最終打開了我的大腦,發現轉儲的返回是一個不是數組的對象,所以我使用$resultsArray = (array)$resultRow;將它轉換爲數組,然後將其運行到表中。

這很髒,但現在正在做我想要的。謝謝@ JI-Web,如果沒有你的幫助,我無法做到。我想我現在正在接受PHP的hang hang。 根據分析結果,將來自第三方來源的相關數據放入框架中。我可能會回來尋求更多幫助!