1

4 つの値を持つテーブルがあり、そのうち 3 つに興味があります。使用している MYSQL クエリは次のとおりです。

Select Product.product_id, Product.cost, Product.image from Product

このクエリをテストしたところ、データベースで動作します。単一の列を取得して値を返す方法は理解できますが、複数の列を理解することはできません。配列を使用してさまざまな列名を格納し、物事を反復して、さまざまな for ループを試しました。私は非常にイライラしており、どうすればよいかわかりません。

これが私の最後の試みでした:

$conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or 
        die('There was a problem connecting to the database');


$stmt = "Select Product.product_id, Product.cost, Product.image from Product";

if(!$result = $conn->query($stmt)){ 
die('there was an error retrieving the information');
}

$tablebuild = "<table>";

while ( $row = $result->fetch_assoc() ){

foreach ($row as $next){
    $tablebuild .= "<tr><td>";
    $tablebuild .= $result['product_id'];
    $tablebuild .= "</td><td>";
    $tablebuild .= $result['cost'];
    $tablebuild .= "</td><td>";
    $tablebuild .= $result['image'];
    $tablebuild .= "</td></tr>";
}

$tablebuild .= "</table>";

明らかに、私はそれをコードの文字列に構築しようとしているので、後で必要なページにエコーすることができます。ただし、このページを実行するたびに、ソース コードのない空白のページしか表示されません。

4

2 に答える 2

2

をなくしてforeach使う$row、ではなく$result

while ( $row = $result->fetch_assoc() ){
    $tablebuild .= "<tr><td>";
    $tablebuild .= $row['product_id'];
    $tablebuild .= "</td><td>";
    $tablebuild .= $row['cost'];
    $tablebuild .= "</td><td>";
    $tablebuild .= $row['image'];
    $tablebuild .= "</td></tr>";
}
于 2013-09-02T01:17:20.037 に答える
0

あなたの問題は、whileループを閉じないことだと思います。また、あなたforeachは正しくありません。これは、必要でなくても次のようにする必要があります。

 $tablebuild .= "<table>";
while ( $row = $result->fetch_assoc() ){

   $tablebuild .= "<tr>";

   foreach ($row as $next){
      $tablebuild .= "<td>$next<td>";
   }

   $tablebuild .= "</tr>";

}

 $tablebuild .= "</table>";
于 2013-09-02T01:21:38.217 に答える