1

So I'm trying to make a HTML table that gets data from a MySQL database and outputs it to the user. I'm doing so with PHP, which I'm extremely new to, so please excuse my messy code!

The code that I'm using is: braces for storm of "your code is awful!"

<table class="table table-striped table-hover ">
    <thead>
    <tr>
        <th>#</th>
        <th>Name</th>
        <th>Description</th>
        <th>Reward</th>
        <th>Column heading</th>
    </tr>
    </thead>
    <tbody>
    <?php
        $con = mysql_connect("localhost", "notarealuser", 'notmypassword');
        for ($i = 1; $i <= 20; $i++) {
            $items = ($mysqli->query("SELECT id FROM `items` WHERE id = $i"));
            echo ("<tr>");
            echo ("
                <td>
                while ($db_field = mysqli_fetch_assoc($items)) {
                    print $db_field['id'];
                }</td>");
            $items = ($mysqli->query("SELECT name FROM `items` WHERE id = $i"));
            echo ("
                <td>
                while ($db_field = mysqli_fetch_assoc($items)) {
                    print $db_field['name'];
                }</td>");
            $items = ($mysqli->query("SELECT descrip FROM `items` WHERE id = $i"));
            echo ("
                <td>
                while ($db_field = mysqli_fetch_assoc($items)) {
                    print $db_field['descrip'];
                }</td>");
            $items = ($mysqli->query("SELECT reward FROM `items` WHERE id = $i"));
            echo ("
                <td>
                while ($db_field = mysqli_fetch_assoc($items)) {
                    print $db_field['reward'];
                }</td>");
            $items = ($mysqli->query("SELECT img FROM `items` WHERE id = $i"));
            echo ("
                <td>
                while ($db_field = mysqli_fetch_assoc($items)) {
                    print $db_field['img'];
                }</td>");
            echo ("</tr>");
        }
    ?>
    </tbody>
</table>

However, this code is not working - it simply causes the page to output an immediate 500 Internal Server Error. IIS logs show it as a 500:0 - generic ISE. Any ideas?

5
  • Be more specific please. Simply asking Anyone have any ideas will not help know what is the problem. Commented Jul 30, 2015 at 10:39
  • It looks like you think you can echo PHP code to the browser to run it there. NO NO No PHP code only runs on the server Commented Jul 30, 2015 at 10:42
  • You should read a little bit. You should have a DB class. I would recommend you to try a light weight framework. You will see everything will workout more smoothly... Commented Jul 30, 2015 at 10:49
  • 500 Internal Server Error means you have an error in PHP. You need to enable error handling on the server so that it shows you what the error is. Otherwise it may be very hard to debug, especially with your knowledge level. Commented Jul 30, 2015 at 10:49
  • So, you don't have time, you don't have knowledge and you decided to use other people's time. Commented Jul 30, 2015 at 16:13

4 Answers 4

2

You are mixing mysql and mysqli, not closing php code block and you are not selecting a database. Plus you don't have to run a query for each field

Try this:

<table class="table table-striped table-hover ">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Description</th>
            <th>Reward</th>
            <th>Column heading</th>
        </tr>
    </thead>

    <tbody>
        <?php
            $con = new mysqli("host","user", "password", "database");

            $execItems = $con->query("SELECT id, name, descrip, reward, img FROM `items` WHERE id BETWEEN 1 AND 20 ");

            while($infoItems = $execItems->fetch_array()){
                echo    "
                            <tr>
                                <td>".$infoItems['id']."</td>
                                <td>".$infoItems['name']."</td>
                                <td>".$infoItems['descrip']."</td>
                                <td>".$infoItems['reward']."</td>
                                <td>".$infoItems['img']."</td>
                            </tr>
                        ";

            }
        ?>
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

Comments

1
<table class="table table-striped table-hover">
      <thead>
          <tr>
              <th>#</th>
              <th>Name</th>
              <th>Description</th>
              <th>Reward</th>
              <th>Column heading</th>
          </tr>
      </thead>
      <tbody>
      <?php
      $con = mysqli_connect("hostname","username",'password');
      $sql= "SELECT * FROM `items` WHERE id <20 ";
      $items = (mysqli_query($sql));
      while ( $db_field = mysqli_fetch_assoc($items) ) {?>
          <tr><td><?php echo $db_field['id'];?></td></tr>
          <tr><td><?php echo $db_field['name'];?></td></tr>
          <tr><td><?php echo $db_field['descrip'];?></td></tr>
          <tr><td><?php echo $db_field['reward'];?></td></tr>
          <tr><td><?php echo $db_field['img'];?></td></tr>
      <?php}
      </tbody>
</table>

Try these, not tested

2 Comments

Try to use mysqli or PDO (currently the code uses both and clearly won't work)
You could take your own advice. You are using mysql_ and mysqli_ that also will not work
1

Where is the question?

There's many problems with this code. First, you are confused between PHP and HTML. Code between is PHP. It's executed on the server, you can have loops and variables and assignments there. And if you want some HTML there you use "echo".

Code outside is HTML - it's sent to the browser as is.

Second - what you seem to be doing is querying each field separately. This is not how you work with SQL.

Here's more or less what you need to do:

//Query all rows from 1 to 20:
$items = $mysqli->query("SELECT id,name,descrip,reward,img FROM `items` WHERE id between 1 and 20");    
//Go through rows    
while ( $row = mysqli_fetch_assoc($items) ) 
{
   echo "<tr><td>{$db_field['id']}</td>";
   //echo the rest of the fields the same way 

});

Comments

0

I'm going to go ahead and assume that the code isn't working and that's because there's several basic errors. I'd strongly suggest doing some hard reading around the topic of PHP, especially since you're using databases, which, if accessed with insecure code can pose major security risks.

Firstly, you've set-up your connection using the procedural mysql_connect function but then just a few lines down you've switched to object-orientation by trying to call the method mysqli::query on a non object as it was never instantiated during your connection.

http://php.net/manual/en/mysqli.construct.php

Secondly, PHP echo() doesn't require the parentheses. PHP sometimes describes it as a function but it's a language construct and the parentheses will cause problems if you try to parse multiple parameters.

http://php.net/manual/en/function.echo.php

Thirdly, you can't simply switch from HTML and PHP and vice-versa with informing the server/browser. If you wish to do this, you need to either concatenate...

echo "<td>".while($db_filed = mysqli_fetch_assoc($item)) {
     print $db_field['id'];
}."</td>;

Or preferably (in my opinion it looks cleaner)

<td>
    <?php 
    while($db_filed = mysqli_fetch_assoc($item)) {
        print $db_field['id'];
    }
    ?>
</td>

However, those examples are based on your code which is outputting each ID into the same cell which I don't think is your goal so you should be inserting the cells into the loop as well so that each ID belongs to its own cell. Furthermore, I'd recommend using echo over print (it's faster).

Something else that may not be a problem now but could evolve into one is that you've used a constant for you FOR loop. If you need to ever pull more than 20 rows from your table then you will have to manually increase this figure and if you're table has less than 20 rows you will receive an error because the loop will be trying to access table rows that don't exist.

I'm no PHP expert so some of my terminology might be incorrect but hopefully what knowledge I do have will be of use. Again, I'd strongly recommend getting a good knowledge of the language before using it.

Comments

Your Answer

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