Php 遍历多行mysqli的列值

Php 遍历多行mysqli的列值,php,database,mysqli,Php,Database,Mysqli,我有一个表,有4个值,其中3个我感兴趣。我使用的MYSQL查询是: Select Product.product_id, Product.cost, Product.image from Product 我已经测试了这个查询,它可以与我的数据库一起工作。我可以找出如何让单个列返回值,但无法找出多个列。我尝试了各种for循环,使用数组存储各种列名并进行迭代。我感到非常沮丧,不知道该怎么办 这是我最后一次尝试: $conn = new mysqli(DB_SERVER, DB_USER, DB_P

我有一个表,有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>";
$conn=新的mysqli(DB\u服务器、DB\u用户、DB\u密码、DB\u名称)或
die('连接到数据库时出现问题');
$stmt=“从产品中选择Product.Product\u id、Product.cost、Product.image”;
如果(!$result=$conn->query($stmt)){
die(“检索信息时出错”);
}
$tablebuild=“”;
而($row=$result->fetch_assoc()){
foreach($row作为$next){
$tablebuild.=“”;
$tablebuild.=$result['product_id'];
$tablebuild.=“”;
$tablebuild.=$result['cost'];
$tablebuild.=“”;
$tablebuild.=$result['image'];
$tablebuild.=“”;
}
$tablebuild.=“”;

显然,我正试图将它构建成一个代码字符串,以便以后可以在需要它的页面中回显它。不过,每次运行此页面时,我只会得到一个没有源代码的空白页面。

丢失
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>";
}
while($row=$result->fetch_assoc()){
$tablebuild.=“”;
$tablebuild.=$row['product_id'];
$tablebuild.=“”;
$tablebuild.=$row['cost'];
$tablebuild.=“”;
$tablebuild.=$row['image'];
$tablebuild.=“”;
}

我认为你的问题在于,你没有关闭
循环,而且你的
foreach
是不正确的,这就是它应该是的方式,即使它不是必需的

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

   $tablebuild .= "<tr>";

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

   $tablebuild .= "</tr>";

}

 $tablebuild .= "</table>";
$tablebuild.=”;
而($row=$result->fetch_assoc()){
$tablebuild.=“”;
foreach($row作为$next){
$tablebuild.=“$next”;
}
$tablebuild.=“”;
}
$tablebuild.=“”;

您是否启用了错误报告功能?我无法成功地“打开”错误报告功能。我一直在一个网站上测试我的代码: