如何从我的数据库中的表中获取所有数据并使用php显示它?

时间:2018-01-08 13:51:56

标签: php mysql database mysqli

我正在尝试显示一个包含6个值的数组,但我不知道该怎么做。我尝试使用num_rows发布数组,但它似乎并没有这样做。我不知道如何解决它。

<?php<br>
//four variables to connect to the database<br>
$host= "localhost";<br>
$username="root";<br>
$password='';<br>
$database="pd00255_coursework2_com1025";<br>

//create a database connection instance
$mysqli = new mysqli($host, $username, $password, $database);<br>

//if there are any values in the table, display them one at a time.
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;<br>
}<br>
echo $mysqli->host_info . "<br>";<br>

$sql = "SELECT ID, Name ,Role, Responsibilities, Age, Date of birth,     Email FROM person";<br>
$result = $mysqli->query($sql);<br>

if ($result->num_rows > 0) {<br>
// output data of each row<br>
while($row = $result->fetch_assoc()) {<br>
  echo "id: " . $row["ID"]. " - ID: " . $row["Name"]. " " . $row["Role"]. " " . $row["Responsibilities"]. "Responsibilities" . $row["Age"]. "Age" .     $row["Date of birth"]. "Date of birth" . $row["Email"]."Email" "<br>";
        }<br>
    } else {<br>
echo "0 results";<br>
}<br>
$mysqli->close;<br>
?><br>

1 个答案:

答案 0 :(得分:0)

您的查询在选择列Date of birth时出错,因为它有空格,请尝试:

"SELECT ID, Name, Role, Responsibilities, Age, `Date of birth`, Email FROM person"