1

私はPHPを使用したPDOを初めて使用しますが、あなたの助けが必要です。selectステートメントを使用して関数を作成しましたが、クエリの値が0より大きい場合は、フェッチを返します。

 $this->result->setFetchMode(PDO::FETCH_OBJ); 

 if ($this->result->rowCount() > 0) {     
   return $this->result->fetch(); 

 }

そして私のメインプログラムで私はこれを持っています

 foreach ($category_results as $row){
        echo .$row['id_category'].' '. $row['name']; 

 }

this command echo only 1 and F twice but if i put only $row it echo all the results correct e.g 1 Food, 2 Drinks. how i can echo the results separately like the example i have above? i want to put them in a list box

thank you

4

1 に答える 1

3

あなたが使用している:

 $this->result->setFetchMode(PDO::FETCH_OBJ); 

したがって、行がオブジェクトに格納されます。マニュアルも参照してください。

それらを取得するには、次を使用できます。

 echo .$row->id_category.' '. $row->name; 

またfetch、1 行のみを取得することに注意してください。すべての行を取得するには、 を使用しますfetchAll。ただし、完全なコードを見ないと、それが必要かどうかわかりません。

于 2012-11-13T21:22:21.033 に答える