Home > Software design >  fetch a text TEXT TYPE from database using pdo
fetch a text TEXT TYPE from database using pdo

Time:06-26

actually i have uploaded text to database but when i fetch it and want to use it in html the text format isn't good as you could see in this image i hope i can get some help.

<?php 
    if(isset($_GET['id'])){
        extract($_GET);
        include('dbconnexion.php');
        try{
            $r=$db->prepare("SELECT * FROM GREENWORK WHERE id_gw=$id");
            $r->execute();
            $tbl=$r->fetch();
        }catch(PDOException $err){
            die("error while selectin ".$err->getMessage());
        }
    }
    ?>    

<div class='container-fluid'>
  <H5>les ingredients :</H5>
  <p>
    <?php print_r($tbl[3]); ?>
  </p>
  <h5>La description:</h5>
  <p>
    <?php echo $tbl[4]; ?>
  </p>
</div>

CodePudding user response:

If your text got cut (length-wise), check the size/length of your text-column in the database. If not VARCHAR but TEXT, try the type MEDIUMTEXT/LONGTEXT.

If your newlines went missing, try using nl2br().

Also please use Prepared Statements to prevent SQL Injection and also escape everything you output (using for example htmlspecialchars() or for attributes htmlentities()) to prevent XSS and format issues.

So your output could look something like this:

 <?php echo nl2br(htmlspecialchars($tbl[4])); ?>
  • Related