How to print multiple values ​​separately in php?

I am fetching data from a database using a unique id. Now the problem is that all values ​​are fetched and printed on one line. I want them to be below each other. I tried "
" "\ n
" as well as nl2br. If anyone could help me. Thanks in advance.

PHP file:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'DB';
$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
die("connection couldn't be established");
if(isset($_POST['EnrNo']) === true && empty($_POST['EnrNo']) === false) {
//$Enr = $_POST['EnrNo'];
$EnrNo = mysql_real_escape_string ($_POST['EnrNo']);
$query = "Select * FROM cert WHERE EnrNo = '$EnrNo'";
    $result = $db->query($query);
    $total_num_rows = $result->num_rows;
    while ($row=$result->fetch_array())
  echo ("EnrNo: " .$row["EnrNo"]);
  echo ("Name: " .$row["Name"]);
  echo ("Batch Code: " .$row["Batch Code"]); 
  echo ("Start Date: " .$row["Start Date"]);
  echo ("End Date: ".$row["End Date"]);
  echo ("Course: " .$row["Course"]);
  echo ("Duration: " .$row["Duration"]);
 }  mysqli_free_result($result);
    } else {
        echo ('Data not found');


HTML file:

<!DOCTYPE html>
Enr No: <input type="text" name="EnrNo" id="EnrNo" /><br/><br />
<input type="submit" name="retrieve" value="retrieve" id="EnrNo-sub" />
<div id="EnrNo-data"></div>
<script src="//"></script>
<script type="text/javascript">

$('input#EnrNo-sub').on('click', function() { 
var EnrNo = $('input#EnrNo').val();
if (EnrNo != '') {
$.post('retrieve.php', {EnrNo: EnrNo}, function(data) { 






source to share

7 replies

Try "
" instead of "\ n", Example:

 echo "Value1  :".$value1."<br>";
 echo "Value2  :".$value2."<br>";
 echo "Value3  :".$value3."<br>";
 echo "Value4  :".$value4."<br>";
 echo "Value5  :".$value5."<br>";




You just tried:

echo ("Duration: " .$row["Duration"]."<br>");



appears only in the source code and <br>

affects the html.



You should use and <br>

for line breaks .html()

instead of .text()


Otherwise the html will be escaped.



The HTML tag uses a tag BR

to insert a line break.

To print the value on a new line, you can add your line break tag <BR />

after each line like this:

echo ("EnrNo: " .$row["EnrNo"]).'<BR />';  



In HTML, the tag <br>

has no end tag.

In XHTML, the tag <br>

must be properly closed, for example:<BR />



try it

  while ($row=$result->fetch_array())
  echo ("EnrNo: " .$row["EnrNo"]."<br>");
  echo ("Name: " .$row["Name"]."<br>");
  echo ("Batch Code: " .$row["Batch Code"]."<br>"); 
  echo ("Start Date: " .$row["Start Date"]."<br>");
  echo ("End Date: ".$row["End Date"]."<br>");
  echo ("Course: " .$row["Course"]."<br>");
  echo ("Duration: " .$row["Duration"]."<br>");

Run codeHide result



try it

foreach($result->result() as $row)


{echo ("EnrNo:". $ row-> EnrNo. "
"); echo ("Name:". $ row-> Name. "
"); echo ("Batch Code:". $ row-> Batch Code. "
"); echo ("Start date:". $ row-> Start date. "
"); echo ("End date:". $ row-> End date. "
"); echo ("Course:". $ row-> Course. "
"); echo ("Duration:". $ row-> Duration. "
"); }



Given your output, you can write a loop for the fields you want to print and just add <br>

to each line; also make sure you remove your HTML output correctly:

foreach (['EnrNo', 'Name', 'Batch Code', 'Start Date', 'End Date', 'Course', 'Duration'] as $field) {
    echo $field, ': ', htmlspecialchars($row[$field], ENT_QUOTES, 'UTF-8'), '<br>';




All Articles