dongyin2643 2016-05-12 13:08
浏览 20

如何使用数组显示数据库中的所有数据

I'm trying to connect $countries to an array that will display all the data from my database, it only displays the last data I entered.Is there anyway I can display all of them?

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT product_id, product_name , quantity FROM inventory";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
        $tom = array(" ". $row["quantity"] . $row["product_name"]);
         $countries = $tom;
     }
} else {
     echo "0 results";
}

$conn->close();
  • 写回答

4条回答 默认 最新

  • douzhang5121 2016-05-12 13:10
    关注

    because you have assinged data to $countries with wrong manner:

    $countries = array();
    if ($result->num_rows > 0) {
         // output data of each row
         while($row = $result->fetch_assoc()) {
            $tom = array(" ". $row["quantity"] . $row["product_name"]);
            $countries[] = $tom; // use []
         }
    } else {
         echo "0 results";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答