PHP - 打印数据库整个表

时间:2015-06-15 13:25:13

标签: php mysql

我在php中尝试了一些东西,几年前没有使用过他,所以我想这些天开始学习它。但我不喜欢通过书本和学校学习它,我想通过基本使用它来学习它,这是最常见的任务等。

我有一个问题,那就是如何使用php在数据库中打印整个表。

我在本地sql server中创建了一个数据库,并通过phpmyadmin修改了表。它是这样的: 数据库:书店 表:书籍

在书籍表中我有11个coulmns,我有一个主键被指定为book_id。我想让我的输出像每个book_id其他列的所有值一样。

例如: book_id 1 标题哈姆雷特 流派悲剧 评分9.8非常好 作者威廉莎士比亚 ...

等等。

这是我在php里面的SQL SELECT

<?php

$host = "localhost";
$user = "admin";
$pass ="ismarhusc1";
$db = "bookstore";

$conn = mysqli_connect($host,$user,$pass,$db) or die("Error " . mysqli_error($link)); 

$sql = "SELECT book_id, title, genre, rating, description, author, author_rating, author_description, author_id  FROM books" or die("Error in the consult.." . mysqli_error($link));
$result = mysqli_query($sql);

现在请告诉我如何继续将此选项输出到页面。

2 个答案:

答案 0 :(得分:0)

尝试mysqli_fetch_all(),它会返回您的所有查询记录。

提示:一般来说,你只想要一个关联数组,你可以通过传递第二个参数作为MYSQLI_ASSOC来实现这个目的

答案 1 :(得分:0)

Well, thanks for the heads up :)

this is the solution :

<?php

$host = "localhost";
$user = "admin";
$pass ="ismarhusc1";
$db = "bookstore";

$conn = mysqli_connect($host,$user,$pass,$db) or die("Error " . mysqli_error($link)); 


$sql = "SELECT book_id, title, genre, rating, description, author, author_rating, author_description, author_id  FROM books" or die("Error in the consult.." . mysqli_error($link));
if ($result = $conn->query($sql)) {

/* fetch associative array */
while ($row = $result->fetch_assoc()) {
    echo  $row["book_id"];
    echo ' - ' ;
    echo $row["title"];
}

/* free result set */
    $result->free();
}   
?>
相关问题