0

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

4
  • 2
    Add $ to ('server', 'user', 'password', 'database') and make sure your file is .php which is why your page comes out looking that way. Commented Dec 3, 2014 at 15:40
  • @Fred-ii- Shouldn't you post that as an answer? Commented Dec 3, 2014 at 15:42
  • @Jonast92 I should but I'm just afraid that it will lead to more questions like "my data isn't showing from DB...". I know the drill all too well ;) Commented Dec 3, 2014 at 15:43
  • 1
    @Jonast92 Ok, I did post it as answer. I'll just wait for the OP to come back. I even spotted another error. Commented Dec 3, 2014 at 15:45

4 Answers 4

5

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.

  • If you're attempting to run this on your own computer, you will need to install a Webserver.

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>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot for your points and help. I have changed the server to $server etc, I've also changed the scope of sql query to include everything. How do you want me to change the file to php? shall I just change the extension? I'm running it on a server, but I'm editing the page and the files with Dreamweaver. Again, thanks for all the help and apologies for my noobishness.
@Sep You're welcome. Yes, you will need to change the extension of your file(s) to .php in order for them to parse properly.
FANTASTIC. Thank you very much @Fred. So the table is updating, but now the problem is that the data isn't showing. I tried to just output the data, but still nothing. I have added an update with the code for checking the date
@Sep Reload my answer and look for Edit near the bottom. Also make sure your column names are correct. "Title" is not the same as "title" etc.
Fred YOU ARE A STAR!!! It worked.. I have to say, I was so stupid. The reason that it was showing blank was because I had previously flushed my database, and hadn't added any data!!! DUH!! Thanks alot. I can't mark the answer as done, so if you can, close the question, or to the admin of this website, please vote up for this answer.
|
0
$mysqli = new mysqli('server', 'user', 'password', 'database');

should be

$mysqli = new mysqli($server, $user, $password, $database);

1 Comment

I would assume that the user hard codes them but changed them before posting so as not to expose sensitive data.
0

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

Comments

0

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 ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.