This question already has answers here:
How can I replace newline or \r\n with <br/>?
(10 answers)
Closed 5 years ago.
I read a text from my database which has multiple lines and and stored in a variable.
when I output this variable on my webpage it is printed all in one line.
How do output in different lines.
<?php
$num=$_GET['id'];
$result=mysql_query("SELECT * FROM messages WHERE id='$num'");
$row=mysql_fetch_array($result);
$msg=$row['message'];
?>
message:<br/><?php echo $msg;?>//Here I need to print message in multiple
//lines as entered in the database.
If you want to show the Exact Message as fetched from the database, Use <pre> tag!Example:
<?php
$num=$_GET['id'];
$result=mysql_query("SELECT * FROM messages WHERE id='$num'");
$row=mysql_fetch_array($result);
$msg=$row['message'];
?>
message:<br/><?php echo "<pre>$msg</pre>";?>
Hope that helps!
Suggestion : Try not to use mysql_* functions as they are Deprecated now! Instead, Use PDO. It will save your script from SQL Injection too!
Related
This question already has answers here:
Preserve Line Breaks From TextArea
(7 answers)
Closed 4 years ago.
This is inside the phpmyadmin:
This is the output after echo:
and this is the code for display output
$joinEvent = mysqli_query($db, "SELECT * FROM event WHERE event.eventID='".$_GET['eventID']."'");
while($row = mysqli_fetch_array($joinEvent))
{
$joinDetails = $row['details'];
}
<p align="justify"><?php echo $joinDetails; ?></p>
How do I echo exactly what user enter in the input?
Use nl2br() when you output to the screen, change your current code to:
<p align="justify"><?php echo nl2br($joinDetails); ?></p>
It might be better for you to store your details column as a text datatype.
Read here: What is the MySQL VARCHAR max size?
Your result is actually exactly what the user input is ; except that in HTML, line break characters (\r\n \n and so) are not interpreted by the browser. As said in the comments, you'll have to use the PHP function nl2br() to convert your line break characters to <br /> tags, which are the HTML line breaks.
This question already has answers here:
how to show the content exactly as it's saved in mysql
(4 answers)
Closed 6 years ago.
I have a form with an input textbox that stores the content in the mysql database.
For example, I write in the input box:
Hello World!
Here is the new line
In the database, I find exactly the same text without any special characters ("here is..." starts in a new line in the database too).
When I call this data in a php echo, it shows it as:
Hello World! Here is the new line
What's wrong? How can I fix it? thanks
You need to pass the database string to nl2br fucntion to get the new line as break rule in HTML.
<?php
$dbSQLValue = "Hello World!\nHere is the new line"; //string retrieved from SQL
echo nl2br($dbSQLValue);
?>
New lines are not converted directly to HTML break line.
Hope this helps.
Open page source in browser and see that newlines are there, but for the browser to display them - they should not be \ns, but <br> tags or be inside a <pre> tag.
So: either convert newlines to brs via nl2br($your_var) or put the whole echo inside a <pre>.
This question already has answers here:
How to pass variables received in GET string through a php header redirect?
(8 answers)
Closed 7 years ago.
I have two pages in php ,one of them is exam page(exam.php) and another is result page(result.php), the result is calculated in exam page and must be sent to result page to display.(I don't have a form)
to send the result, Inside exam.php ,I write, header("location:result.php?result");
and to get the result inside result.php ,I write, $newresult=$_GET['result'];
but I receive error,and result didn't sent to the result page.
would you please guide me?
Using a URL to pass parameters can be done like so.
HTML
<a href='yourPage.php?name=Script47'>Send Variable</a>
PHP
<?php
if (isset($_GET['name') && !empty(trim($_GET['name'])) {
$name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES);
}
?>
Explanation
The HTML is fairly simple, we create a link which holds a parameter specified after the page extension (?name=[...]).
The PHP first checks if the name parameter which was passed isset to prevent an undefined index error, and we check if it isn't empty. The trim function removes white spaces so an string with a space isn't outputted (" "). When we know that the string has a value in it we sanitize it (never trust user input) and then we output it.
Reading Material
htmlspecialchars();
trim();
empty();
isset();
Try use session
exam.php
<?php
session_start();
$_SESSION['result'] = $result;
?>
result.php
<?php
session_start();
echo $_SESSION['result'];
?>
This question already has answers here:
php echoing angle brackets
(4 answers)
Closed 9 years ago.
Example data in my database:
blabla<blabla
I use phpmyadmin and can see that the data has been input successfully.
However when I try to display the data what I get is:
blabla NOT blabla<blabla
In other words, everything after the < symbol does not display.
<?
while ($mouselist_row = mysql_fetch_array($mouselist)) {
$mouselist_commonstrain = mysql_real_escape_string($mouselist_row['Common_Strain']);
echo "$mouselist_commonstrain.";
}
?>
I tried using mysql_real_escape_string.
Is there something in particular needed to display the <?
thanks
You want something like:
echo htmlspecialchars($mouselist_commonstrain);
(It needs to be HTML escaped.)
try this
$mouselist_commonstrain = stripslashes(htmlspecialchars($mouselist_row['Common_Strain']));
Your problem isn't escaping SQL but HTML. As answered in this question you can use htmlspecialchars function.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between single quote and double quote string in php
Hey I'm having trouble inserting page content into my database.
I'm trying to store:
<p class=\"heading_large\"><?php echo $Topic2C2A[data]; ?></p>
Using this code:
$sql="UPDATE event SET
data='<p class=\"heading_large\"><?php echo $Topic2C2A[data]; ?></p>'
WHERE id='2'";
But when I look at the table all I see is:
<p class="heading_large"><?php echo ; ?></p>
I've obviously escaped the HTML with slashes, is there something similar I need to do with the PHP so $Topic2C2A[data] is displayed?
I would suggest you write your $sql as:
$sql="UPDATE event SET data='<p class=\"heading_large\">".$Topic2C2A[data]."</p>' WHERE id='2'";
Your issue is related to the fact PHP is processing variables inside " (double) quotes.
You can change quotes to ' (single) or another option is to change $Topic2C2A[data] to \$Topic2C2A[data].
Did you try mysqli_real_escape_string()? It should return a fully escaped String!