I'm trying to get data from my database, I use XAMPP local server
it doesn't display student table into html table
it displays: '; } ?>
and the empty table
database name: test, User name: root, no password, table name: student
any response will be appreciated
here is my code:
<!DOCTYPE html>
<html>
<style>
td,th {
border: 1px solid black;
padding: 10px;
margin: 5px;
text-align: center;
}
</style>
<?php
$mysqli = new mysqli("localhost","root","","test")
or die('Error connecting to MySQL server.');;
$query = "SELECT * FROM student" ;
$result = mysqli_query("test", $query);
$row = mysqli_fetch_array($result);
while ($row = mysqli_fetch_array($result))
{
echo $row['root'] . ' ' . $row[''] . '<br />';
}
?>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Branch</th>
<th>Roll Number</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($row))
foreach($row as $rows)
{
?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['branch']; ?></td>
<td><?php echo $rows['roll_no']; ?></td>
</tr>
</tbody>
</table>
</body>
</html>
CodePudding user response:
The foreach
loop has no ending. You need to add a closure after the row ends:
</tr>
<?php
}
?>
</tbody>
Also, you have two simicolons on line 1, but that shouldn't change anything.
Edit:
Take a look at this example file:
<div>
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $value)
{
?>
<div><?php print($value * 2); ?></div>
<?php
}
?>
</div>
This correctly outputs:
<div>
<div>2</div>
<div>4</div>
<div>6</div>
<div>8</div>
</div>
But without the closing <?php } ?>
for each number, there is a 500 error.
CodePudding user response:
This is how you should iterate the query result.
<!DOCTYPE html>
<html>
<style>
td,
th {
border: 1px solid black;
padding: 10px;
margin: 5px;
text-align: center;
}
</style>
<body>
<?php
$mysqli = new mysqli("localhost","root","","test")
or die('Error connecting to MySQL server.');;
$query = "SELECT * FROM student" ;
$result = mysqli_query("test", $query);
?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Branch</th>
<th>Roll Number</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<?php echo $row['branch']; ?>
</td>
<td>
<?php echo $row['roll_no']; ?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
CodePudding user response:
Let's take a look at the first section
<?php
$mysqli = new mysqli("localhost","root","","test")
or die('Error connecting to MySQL server.');;
$query = "SELECT * FROM student" ;
$result = mysqli_query("test", $query);
$row = mysqli_fetch_array($result);
while ($row = mysqli_fetch_array($result))
{
echo $row['root'] . ' ' . $row[''] . '<br />';
}
?>
The function for mysqli_query
takes in a MySQLI resource as the first parameter and the query as the second. It looks like you've got "test" instead of $mysqli
When you're calling $row = mysqli_fetch_array($result);
right below, that's using the response and advancing the internal pointer to the next row, so the next time you call mysqli_fetch_array, it'll return back with the second row! After the while-loop, you've now gone through the entire result set, and $row
will only be false (or null).
Finally, there is the case of a missing closing } for your foreach-loop.
Based on those, this is one possible way of showing the data, assuming 'name', 'branch' and 'roll_no' are columns on the student table.
<?php
$mysqli = new mysqli("localhost", "root", "", "test")
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM student";
$result = mysqli_query($mysqli, $query);
?>
<!DOCTYPE html>
<html>
<style>
td,th {
border: 1px solid black;
padding: 10px;
margin: 5px;
text-align: center;
}
</style>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Branch</th>
<th>Roll Number</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_array($result)): ?>
<tr>
<td><?= htmlspecialchars($row['name']) ?></td>
<td><?= htmlspecialchars($row['branch']) ?></td>
<td><?= htmlspecialchars($row['roll_no']) ?></td>
</tr>
<?php endwhile ?>
</tbody>
</table>
</body>
</html>