4

I am a beginner to do website development. And I have inserted some data into database by phpMyAdmin. Here is the database:

Database Screenshot

And these are my code to generate a table in web.

<table border = "1" cellpadding = "5" cellspacing = "0">
    <tr style="text-align: left;">
        <th style="width : 150px;">pmID</th>
        <th style="width : 150px;">pubYear</th>
        <th style="width : 150px;">pubMonth</th>
        <th style="width : 150px;">pubDay</th>
        <th style="width : 150px;">pubTitle</th>
        <th style="width : 150px;">articleTitle</th>
        <th style="width : 300px;">abstract</th>
    </tr>
    
    
    <?php
    while($row = $result -> fetch_object()){
        $pmID = htmlentities($row->pmID, ENT_QUOTES, "UTF-8");
        $pubYear = htmlentities($row->pubYear, ENT_QUOTES, "UTF-8");
        $pubMonth = htmlentities($row->pubMonth, ENT_QUOTES, "UTF-8");
        $pubDay = htmlentities($row->pubDay, ENT_QUOTES, "UTF-8");
        $pubTitle = htmlentities($row->pubTitle, ENT_QUOTES, "UTF-8");
        $articleTitle = htmlentities($row->articleTitle, ENT_QUOTES, "UTF-8");
        $abstract = htmlentities($row->abstract, ENT_QUOTES, "UTF-8");
        
    ?>
    
        <tr>
            <td><?php echo $pmI;?></td>
            <td><?php echo $pubYear;?></td>
            <td><?php echo $pubMonth;?></td>
            <td><?php echo $pubDay;?></td>
            <td><?php echo $pubTitle;?></td>
            <td><?php echo $articleTitle;?></td>
            <td><?php echo $abstract;?></td>    
        </tr>
    
    <?php
    }
    ?>

And the result: Result Table

Noticed that some columns are empty. I don't know how to fix it.

Francisco
  • 10,918
  • 6
  • 34
  • 45
  • 2
    Although I can't see why, seems like your issue is with your use of `htmlentities`, the missing cases either have an HTML-like tag (``) or an apostrophe (`'`). Try to `var_dump($row)` and see if you get the correct values before running them in `htmlentities`. – Growiel May 04 '17 at 09:33
  • Just for a quick check remove ```ENT_QUOTES, "UTF-8"``` from the field where you are getting the blank line & try to print it. – Suresh May 04 '17 at 09:33

4 Answers4

0

Possible of special character in the db cause it. My suggestion, try avoid using any special characters and replace it with normal characters. Hope this help you.

JunieL
  • 87
  • 10
0

Use foreach to loop the data

<?php
    foreach ($result as $row) {
?>
      <tr>
        <td><?php echo $row->pmI;?></td>
        <td><?php echo $row->pubYear;?></td>
        <td><?php echo $row->pubMonth;?></td>
        <td><?php echo $row->pubDay;?></td>
        <td><?php echo $row->pubTitle;?></td>
        <td><?php echo $row->articleTitle;?></td>
        <td><?php echo $row->abstract;?></td>    
      </tr>
<?php
    }
?>

Hope it can help

0

You have to verify that the supplied strings from database to htmlentities method are in UTF-8.

Walid Ammou
  • 390
  • 1
  • 6
  • 21
0

You can solve it various way I think you should read When Should One Use HTML Entities. now your possible solution is

  1. save your source file as UTf-8
  2. The string supplies it as UTF-8.

You can also print variables using echo and print. Also don't store special character in database.

If you want to remove special character try this function $string = preg_replace('/[\x00-\x1F\x7F]/u', '', $string);

Community
  • 1
  • 1
Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33