2016-06-22 10 views
0

私は比較的新しいPHPです。OOPに書き込んだ検索エンジンを転送する過程で午前中です。最初のクエリ結果にループが掛かっています

関連SQL接続コード/ sqlconnect1クラス:ページがロードされるたびに

$search = new LoadSearch; 
$sqlconnect1 = new sqlconnect1; 
$sqlconnect1->query("SELECT * FROM data1 WHERE address_city LIKE 'austin' AND address_zip LIKE '78758'"); 

    //Assign Variables and Display Record Results 
    while ($row1 = $sqlconnect1->resultset()){ 
    $Name1=$row1['name']; 
    $Address1=$row1['address_1']; 
    $City1=$row1['address_city']; 
    $State1=$row1['address_state']; 
    $ZIP1=$row1['address_zip']; 
    $Country1=$row1['address_country']; 
    $Phone1=$row1['phone']; 
    $Website1=$row1['website']; 
    $Category_11=$row1['category_1']; 
    $Category_21=$row1['category_2']; 
    echo" 

    <!--Table 2--> 

    <table class='table2'> 

     <tr> 
      <td></td><td><b>$Name1</b></td> 
     </tr> 

     <tr> 
      <td></td><td>$Address1, $City1, $State1 $ZIP1</td> 
     </tr> 

     <tr> 
      <td></td><td>$Country1</td> 
     </tr> 

     <tr> 
      <td></td><td>$Phone1</td> 
     </tr> 

     <tr> 
      <td></td><td>$Category_11</td> 
     </tr> 

    </table>"; 

、それは無限に最初のクエリ結果を繰り返し、最初の結果を超えて移動したことがない:

public function query($query){ 
    $this->stmt = $this->dbh->prepare($query); 
    } 

    public function bind($param, $value, $type = null){  
     if (is_null($type)) { 
      switch (true) { 
       case is_int($value): 
        $type = PDO::PARAM_INT; 
        break; 
       case is_bool($value): 
        $type = PDO::PARAM_BOOL; 
        break; 
       case is_null($value): 
        $type = PDO::PARAM_NULL; 
        break; 
       default: 
        $type = PDO::PARAM_STR; 
      } 
     } 

     $this->stmt->bindValue($param, $value, $type); 
    } 
    public function execute(){ 
     return $this->stmt->execute(); 
    } 

    public function resultset(){ 
     $this->execute(); 
     return $this->stmt->fetch(PDO::FETCH_ASSOC); 
    } 

Whileループ。アドバイスをいただければ幸いです。それが手続き型のときも同様の設定がうまくいきました。

+1

$sqlconnect1->execute()をコールする必要がありますか? :) – 0x13a

+0

@Diego Mariani私が言ったように、私は新しいです。私は人々が通常それをデータベースと名付けると思います。コードの最初のセグメントは、すべてのデータベース接続情報を持つsqlconnect1クラスからのものです。 –

+0

@sectus私はfetchAllを試しましたが、データベース内のすべてを返すようです。テーブルが読み込まれるので、私はそれを仮定しているだけですが、決してデータでいっぱいになりません。それは、テーブルを何も持たずに無期限にページを繰り返すだけです。 –

答えて

3

$this->execute()呼び出しを削除します。これは、すべてのループ繰り返しを呼び出すためです。execute、次にfetchです。このように、あなたは(最初の行)は常に同じ結果を取得するつもりだ

public function resultset(){ 
    // $this->execute(); -- Remove this line 
    return $this->stmt->fetch(PDO::FETCH_ASSOC); 
} 

はもちろん、あなたのwhileループ地獄 `sqlconnect1`クラスが何であるか

+0

ありがとう! –

+0

ようこそ。) – 0x13a

関連する問題