我试图在MySQL数据库的网页上显示一个表,但它现在正在工作!这是我的代码:
<?php
function list_schools() {
$conn = mysqli_connect("localhost", "root", "", "Database_Project");
if ($conn === false) {
die("Could not connect:" . mysqli_connect_error());
}
$output = "";
$result = mysqli_query($conn, "SELECT school_id, name, address, phone_number, email, type FROM Schools;") or die('cannot show tables');
while ($row = mysqli_fetch_array($result)){
$output .= '
<tr>
<td>' . $row['school_id'] . '</td>
<td>' . $row['name'] . '</td>
<td>' . $row['address'] . '</td>
<td>' . $row['phone_number'] . '</td>
<td>' . $row['email'] . '</td>
<td>' . $row['type'] . '</td>
</tr>';
}
return $output;
}
$exec_func = list_schools();
?>
<table cellpadding="0" cellspacing="0" width="100%" class="sortable">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>
<th>Email</th>
<th>Type</th>
<td> </td>
</tr>
</thead>
<tbody>
<?php echo exec_func; ?>
</tbody>
</table>
每当我尝试在浏览器上显示它时,我都会得到以下信息:
1
无法显示1
中的列我做错了什么?
答案 0 :(得分:0)
从您的查询中,很明显您不会获得ID
列,而是school_id
。
因此,在构建$output
时只需更正您的陈述:
<td>' . $row['school_id'] . '</td>
答案 1 :(得分:0)
为什么不尝试:
<?php
function list_schools() {
$conn = mysqli_connect("localhost", "root", "", "Database_Project");
if ($conn === false) {
die("Could not connect:" . mysqli_connect_error());
}
$output = "";
$result = mysqli_query($conn, "SELECT school_id, name, address, phone_number, email, type FROM Schools;") or die('cannot show tables');
}
$exec_func = list_schools();
?>
<table cellpadding="0" cellspacing="0" width="100%" class="sortable">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>
<th>Email</th>
<th>Type</th>
<td> </td>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_array($result)){
echo '
<tr>
<td>' . $row['school_id'] . '</td>
<td>' . $row['name'] . '</td>
<td>' . $row['address'] . '</td>
<td>' . $row['phone_number'] . '</td>
<td>' . $row['email'] . '</td>
<td>' . $row['type'] . '</td>
</tr>'; } ?>
</tbody>
</table>
答案 2 :(得分:0)
您可以使用以下代码
<table cellpadding="0" cellspacing="0" width="100%" class="sortable">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>
<th>Email</th>
<th>Type</th>
<td> </td>
</tr>
</thead>
<tbody>
<?php
$conn = mysqli_connect("localhost", "root", "", "Database_Project");
if ($conn === false) {
die("Could not connect:" . mysqli_connect_error());
}
$results = $mysqli->query("SELECT school_id, name, address, phone_number, email, type FROM Schools");
while($obj = $results->fetch_object()) {
?>
<tr>
<td> <?php echo $row['school_id']; ?> </td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo $row['address']; ?></td>
<td> <?php echo $row['phone_number']; ?></td>
<td> <?php echo $row['email']; ?></td>
<td><?php echo $row['type']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
答案 3 :(得分:0)
你的问题在于:
$result = mysqli_query($conn, "SELECT school_id, name, address, phone_number, email, type FROM Schools;") or die('cannot show tables');
您无法在此处使用or
运算符。
请查看此Logical Operators以获取解释。
示例:
echo 't' or 'f'; //will output 1
使用此代码:
$result = mysqli_query($conn, "SELECT school_id, name, address, phone_number, email, type FROM Schools;")
$num_rows = mysqli_num_rows($result);
if (!$num_rows) {
die('cannot show tables');
}