我是初学者,我无法弄清楚如何使用php在网页上列出mysqli表中的产品。目前我只是设法使用以下代码获取第一行重复表中存在的行数:
<?php
$p_sql = "SELECT * FROM products";
$p_query = mysqli_query($db_conx, $p_sql);
$productData = mysqli_fetch_array($p_query, MYSQL_ASSOC);
$num = mysqli_num_rows($p_query);
?>
<!DOCTYPE html>
<?php print "There are currently $num rows in the table<P>";
for ($row=0; $row<$num; $row++){
$name = $productData["product_name"];
echo "$name <br>";
};
?>
我知道在我的for循环中我没有包含$row
但我不知道如何正确地包含它。非常感谢您的帮助!
答案 0 :(得分:1)
不要使用for()
循环,而是使用while()
$productData = mysqli_fetch_array($p_query, MYSQL_ASSOC)
循环
<?php
$p_sql = "SELECT * FROM products";
$p_query = mysqli_query($db_conx, $p_sql);
$num = mysqli_num_rows($p_query);
?>
<!DOCTYPE html>
<?php print "There are currently $num rows in the table<P>";
while($productData = mysqli_fetch_array($p_query, MYSQL_ASSOC)){
echo $productData["product_name"]."<br />";
};
?>