I am trying to display a table on a webpage from MySQL database, but it's now working! Here's my code:
<?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>
Whenever I try displaying it on my browser I get the following:
1
cannot show columns from 1
What am I doing wrong?
From your query, it's pretty obvious that you'll not get ID
column, but school_id
.
So merely correct your statement when building $output
:
<td>' . $row['school_id'] . '</td>
Why not try:
<?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>
You can use this following code
<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>
Your problem lies here:
$result = mysqli_query($conn, "SELECT school_id, name, address, phone_number, email, type FROM Schools;") or die('cannot show tables');
you cannot use or
operator here. Check this Logical Operators for explanation.
example:
echo 't' or 'f'; //will output 1
use this code:
$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');
}