Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php echo from database with break lines

Tags:

sql

php

echo

This is a simple question I believe, but can't figure it out yet. I have a text area that after submit goes to a database, and then I echo this text on a page, but here is the problem, say the person writes on the textarea:

Hi Robert,
This is just a test!.
Jason.

And the message goes to the database just like that, but when I echo that, I get:

Hi Robert, This is just a test!. Jason.

This is the form:

<textarea name="newMessage" wrap="hard" cols="30" rows="3"></textarea>

<input type="submit" name="submit" value="Ingresar"> </>

This is the code I use to display the text:

<?php   
    while($row = mysql_fetch_assoc($messages)){
        echo $row['mensaje']."<br/>";
    }
?>

This is what I use to insert the code:

if(isset($_POST['submit'])){        

            $check4LB = $_POST['newMessage'];
            while($letter = mysql_fetch_assoc($check4LB)){
                if($letter=' '){
                $letter='<br/>';
                }
            } /////I know this is not write bu is the idea i thgouht at least

            $query = mysql_query("SELECT (ifnull(max(idRegistro),0) + 1) as id FROM messages");
            $row = mysql_fetch_array($query);
            $idMax = $row['id'];     
            $insertMessage = "INSERT INTO messages(idRegistro, mensaje) VALUES ('".$idMax."','".$letter."')";

             mysql_query($insertMessage) or die(mysql_error());
             echo "<meta http-equiv=Refresh content=\"0 ; url=".$_SERVER['PHP_SELF']."\">"; 
   }
like image 888
Gmo Avatar asked Apr 20 '11 02:04

Gmo


People also ask

How to add break in PHP echo?

Answer: In PHP to echo a new line or line break use '\r\n' or '\n' or PHP_EOL.

How to put a line break in PHP output?

The nl2br() function inserts HTML line breaks (<br> or <br />) in front of each newline (\n) in a string.

How to create a break in PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.

How do you break a line in a database?

SQL Server ' AS 'New Line' -- using carriage return: CHAR(13) SELECT 'First line. '+ CHAR(13) + 'Second line. ' AS 'New Line' -- Using both: CHAR(13)+CHAR(10) SELECT 'First line. '+ CHAR(13)+CHAR(10) + 'Second line.


1 Answers

try echo nl2br($row['mensaje']);

like image 147
Sabeen Malik Avatar answered Oct 07 '22 13:10

Sabeen Malik