I am trying to create a table in a HTML Document using php, and here is my code:
<p>Below is the table of all the books available: <br />
<table border="1" ><th>Book ID</th><th>Subject</th><th>Title</th><th>Print Edition</th><th>Condition</th><th>Asking Price</th>
<?php
$server= 'MYSERVER';
$user = 'MYUSERNAME';
$password = 'MYPASSWORD';
$database = 'MYDBNAME';
$mysqli = new mysqli('server', 'user', 'password', 'database');
foreach ( $mysqli->query('SELECT user,host FROM mysql.user') as $row )
{?>
<tr><td><?php echo $res['ID'];?></td><td><?php echo $res['Subject'];?></td><td><?php echo $res['Title'];?></td><td><?php echo $res['PrintEdition'];?></td><td><?php echo $res['Cond'];?></td><td><?php echo $res['Price'];?></td></tr>
<?php }
mysqli_close($mysqli);
?>
</table>
<br />
If you like to request any of these titles, Do the following:
. . . .
However, it seems I'm not sure what is happening, but I am having this as the outcome on my Chrome (this is the actual screen shot of my webpage):
Below is the table of all the books available: query('SELECT * FROM entry_database') as $row ) {?> Book ID Subject Title Print Edition Condition Asking Price
(The above row looks like a table)
If you like to request any of these titles, Do the following:
I'm not sure why this is happening, but I have a hunch that it is because HTML treats the -> as a tag. Is there any way of fixing this?
Just to explain, I have looked at all the answers, tried all the answers, and as far as I am aware, I can't find the answer.
Many thanks in advance.
UPDATE 1:
So I managed to sort out the additional code showing, thanks to @Fred -ii-
However, the data is not showing. I tried the following to output the date, but no success:
foreach ( $mysqli->query('SELECT * FROM entry_database') as $row )
{?>
<?php echo $row['ID'];?><br /><?php echo $row['Subject'];?><br /><?php echo $row['Title'];?><br /><?php echo $row['PrintEdition'];?><br /><?php echo $row['Cond'];?><br /><?php echo $row['Price'];?><br />
<?php }
mysqli_close($mysqli);
?>
We are getting there, I am sure
Ok, I'll make this an answer:
Add $
to ('server', 'user', 'password', 'database')
which should read as($server, $user, $password, $database)
otherwise PHP will throw you a message about "Undefined constants...".
You need to make sure your file is .php
which is why your page comes out looking that way, as code instead of being properly parsed.
You can instruct Apache to treat .html
files as PHP, but that's just more work.
This I am highly suspecting is what you're doing, trying to run an .html
file and expect it to run it as PHP just as a web browser would for HTML files.
Plus, you're doing as $row
and echoing echo $res
that should be as $res
You're also only selecting the "user" and "host" columns from SELECT user,host
and wanting to echo ID
and Subject
and Title
and PrintEdition
and Cond
and Price
which won't work.
You need to either add/select all those columns, or do SELECT *
and make absolutely sure that those columns do in fact exist in your table.
Edit:
Replace this block:
foreach ( $mysqli->query('SELECT user,host FROM mysql.user') as $row ) {
?>
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['Subject'];?></td>
<td><?php echo $row['Title'];?></td>
<td><?php echo $row['PrintEdition'];?></td>
<td><?php echo $row['Cond'];?></td>
<td><?php echo $row['Price'];?></td>
</tr>
with:
$results = $mysqli->query('SELECT * FROM entry_database');
while($row = $results->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['Subject'];?></td>
<td><?php echo $row['Title'];?></td>
<td><?php echo $row['PrintEdition'];?></td>
<td><?php echo $row['Cond'];?></td>
<td><?php echo $row['Price'];?></td>
</tr>
or:
<!DOCTYPE html>
<head></head>
<body>
<table style="width: 100%;" border="1" cellspacing="5" cellpadding="5">
<thead>
<tr>
<th align="left" valign="middle">ID</th>
<th align="center" valign="middle">Subject</th>
<th align="center" valign="middle">Title</th>
<th align="left" valign="middle">PrintEdition</th>
<th align="center" valign="middle">Cond</th>
<th align="center" valign="middle">Price</th>
</tr>
</thead>
<tbody>
<?php
$server= 'MYSERVER';
$user = 'MYUSERNAME';
$password = 'MYPASSWORD';
$database = 'MYDBNAME';
$mysqli = new mysqli($server, $user, $password, $database);
$results = $mysqli->query('SELECT * FROM entry_database');
while($row = $results->fetch_assoc()): ?>
<tr>
<td align="center" valign="middle"><?php echo $row['ID']; ?></td>
<td align="center" valign="middle"><?php echo $row['Subject']; ?></td>
<td align="left" valign="middle"><?php echo $row['Title']; ?></td>
<td align="center" valign="middle"><?php echo $row['PrintEdition']; ?></td>
<td align="center" valign="middle"><?php echo $row['Cond']; ?></td>
<td align="left" valign="middle"><?php echo $row['Price']; ?></td>
</tr>
<?php endwhile;?>
</tbody>
</body>
</html>
$mysqli = new mysqli('server', 'user', 'password', 'database');
should be
$mysqli = new mysqli($server, $user, $password, $database);
Looks to me you're using $row as the loop variable, but then inside the loop you're referring to $res. Also, you need to execute the query in mysqli then bind the result, then do the loop. Refer to example in the documentation: http://php.net/manual/en/mysqli-result.fetch-assoc.php#example-1812
your code has to be like this:
<p>Below is the table of all the books available: <br />
<table border="1" >
<thead>
<tr>
<th>Book ID</th>
<th>Subject</th>
<th>Title</th>
<th>Print Edition</th>
<th>Condition</th>
<th>Asking Price</th>
</tr>
</thead>
<tbody>
<?php
$server= 'MYSERVER';
$user = 'MYUSERNAME';
$password = 'MYPASSWORD';
$database = 'MYDBNAME';
$mysqli = new mysqli($server, $user, $password, $database);
foreach ( $mysqli->query('SELECT user,host FROM mysql.user') as $row ) {
?>
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['Subject'];?></td>
<td><?php echo $row['Title'];?></td>
<td><?php echo $row['PrintEdition'];?></td>
<td><?php echo $row['Cond'];?></td>
<td><?php echo $row['Price'];?></td>
</tr>
<?php
}
?>
<br />
If you like to request any of these titles, Do the following:
in the foreach you defined the values in a variable $row
then you used the values $res, that's not gonna work
also the mysqli connection was wrong defined.
I tested the code and this code is working for me ;)