【问题标题】:How can I get all my data from a table in my database and show it using php? [duplicate]如何从数据库中的表中获取所有数据并使用 php 显示? [复制]
【发布时间】:2018-01-08 13:51:56
【问题描述】:

我正在尝试显示一个包含 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>

【问题讨论】:

  • 输出的是什么?

标签: php mysql database mysqli


【解决方案1】:

你的查询有错误,选择列Date of birth时,因为有空格,试试:

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

【讨论】:

  • 感谢修复
猜你喜欢
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
  • 2013-10-29
  • 1970-01-01
  • 2015-11-26
相关资源
最近更新 更多