Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP Formatting MySQL Data with PHO

MysticKnight

New Coder
I’m new to PHP, but this seems like a simple question, but it has been difficult to find the answer…

When someone submits a couple of paragraphs with textarea using an online form, I do I display that MySQL information as two paragraphs at the website?
 
You're not being very clear. What does MySQL have to do with paragraphs, textarea and an online form on your website ? Please show us how far you got and where you got stuck.
 
Sorry about that…

The visitor submits 2 paragraphs in a textarea using an online form.

That information is added to the MySQL database. Within the MySQL database, the paragraphs are still separate because there are invisible characters within the paragraphs (\r\n).

If I use PHP to make a call to the database to get those 2 paragraphs, they are displayed at my website as one paragraph because browsers don’t recognize \r or \n, so I assume I have to write a function that adds the necessary HTML tags.

Does that make sense?
 
Yes that makes sense. Though I am very surprised that your paragraphs coming from a web form are being separated by \r\n. This is the Windows specific line terminator (Linux would just have\n). Are you sure it's stored in the database as \r\n ? Specifically, have you hexdumped or otherwise inspected the record to verify that ? I can't think it comes from the webpage like that, so maybe it's PHP or (perhaps more likely) your MySQL connector doing that. Anyway, what I'd try is use the PHP str_replace function to replace \r\n with <p>, which seems the right thing to do for paragraphs. Either on the way in to the database or on the way out (depending in how you want it stored.
 
First, thanks for your reply.

Yeah, it is coming in as \r\n.

I’ll try your suggestion.

I’m very surprised that this isn’t a typical need for anyone that is using PHP with MySQL.

All I want to do is retain separate paragraphs submitted to the MySQL database when I display that information at my website. It only happens at other websites millions of times each day. 😀

I swear I thought I would find step by step instructions easily, but I’m several hours into this one problem and there’s still no definitive answer.
 
The \r\n still baffles me. This is totally unlike anything I've seen on the web. I assume your PHP and MySQL are running on Windows ? That replace trick may well work for you but I feel like we're skirting around the problem rather than understanding and fixing it.
 
It’s running locally and remotely on an Apache server.

It’s probably because I’m using FILTER_SANITIZE_STRING and I’m using prepared statements to send data to the database. But that has to be done to prevent SQL injections.
 
I don't think it is because of that. But you could quickly rule this out I guess.
Still puzzled by the \r\n ... it if were normal for this to appear then many people would have this issue and you would have found a way around it. Are you going for the replacement as I suggested, or dig for the root cause ?
 
Back
Top Bottom