Hi everyone and thanks for taking the time to read.

There's a specific section of my website that I would like to be dynamic. It has the possibility of having one or two paragraphs, possibly even more. What I had done was set up a MySQL database with a field in a table called "description" and then in that field, I typed text literally like this:

<p>hello everyone</p><p>my name is Anthony</p>

Now when I use

<?php echo $row['description']; ?>

then those two paragraphs will appear correctly in the browser window, but in the source, PHP will have echoed them out on the same code line. I want this to be neater, and not have one massively long line of code.

At first I thought I would need to put each paragraph in an individual field of the table, and echo them all out individually but that just sounds silly, there must be a better approach than what I'm doing.

Can anyone help me?
Any help is appreciated.

Recommended Answers

All 10 Replies

If you type this into a text editor with a line break and then copy/paste it into the database, this will also be moved into the SQL field and PHP will echo a new line as well as the code.

Another thing, when getting HTML from the db, you will want to think about how you are doing this, currently with the echo statement you have you may encounter problems with quotes (') if you use them later. I would suggest looking into storing HTML in a database as well as the stripslashes/addslashes functions.

If you type this into a text editor with a line break and then copy/paste it into the database, this will also be moved into the SQL field and PHP will echo a new line as well as the code.

Another thing, when getting HTML from the db, you will want to think about how you are doing this, currently with the echo statement you have you may encounter problems with quotes (') if you use them later. I would suggest looking into storing HTML in a database as well as the stripslashes/addslashes functions.

Hi Xan, thank you for the very quick reply. I'll try that now and hopefully post my success!

Storing HTML in a database, are you meaning about how to escape characters with slashes? Just off the top of my head I'm assuming the addslashes function will take nasty characters like " and add a slash in front of them (which is used for cleaning a variable to be INPUT into the database?) and then stripslashes is to be used when they come back out of the database so that all the evil characters like " are back again, is that right?

ust off the top of my head I'm assuming the addslashes function will take nasty characters like " and add a slash in front of them (which is used for cleaning a variable to be INPUT into the database?) and then stripslashes is to be used when they come back out of the database so that all the evil characters like " are back again

Absolutely. But check if get_magic_quotes_gpc is turned on. If it is turned on, then you don't have to explicitly escape '.

Absolutely. But check if get_magic_quotes_gpc is turned on. If it is turned on, then you don't have to explicitly escape '.

Great stuff. I just tried the suggestion by Xan, and it works brilliantly but i'm not sure how MySQL recognises that there are line breaks, and how PHP interprets them. If I open the database with phpmyadmin and look at the field, I can see line breaks but no code for them, does php just take it as a given? I would have thought I needed to do \r\n, but php took that as literal text when it came out of the database :(

You can make use of nl2br to convert \n (new line) to <br />.

You can make use of nl2br to convert \n (new line) to <br />.

Thanks for all your help, you've got me on my way. You too Xan. Thanks guys!

you are welcome :)

In response to the point about add/strip slashes, here is an example:

You have HTML tags and attributes you want to add to the db, such as <a href='somelink' title='sometitle'> Using the query to get the data would pick up the ' in the string and return an error as some of the content of the data would be interpreted as PHP, not good.

when you add the data to the database either use the addslashes function to change ' to \' so that PHP ignores them when reading from the db or do the same manually.

When you echo the data back, change the echo($data) to echo(stripslashes($data)).

magic_quotes is not the best function out there as it can cause slashes to be added in places they are not needed. read http://en.wikipedia.org/wiki/Magic_quotes for details on that subject.

The linebreaks can't be seen as most software packages hide them by default (they look like a backwards P and a T) PHP however will pick them up and use them in a similar way to a newline command.

magic_quotes is not the best function out there as it can cause slashes to be added in places they are not needed. read http://en.wikipedia.org/wiki/Magic_quotes for details on that subject.

Hmm.. Thats a good link ! But, I would rather use mysql_real_escape_string than addslashes and stripslashes because if you use mysql_real_escape_string, you dont have to worry about stripslashes.

I can't say I have heard of that function before, may be worth looking into for future projects :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.