Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:25:03+00:00 2026-06-09T16:25:03+00:00

I am working on a c# project where I store a file in a

  • 0

I am working on a c# project where I store a file in a database into a medium blob field, the data is being stored fine and when I view it inside MySQL Workbench each line in the file is being shown on it own line in the database, i.e. the same format.

However, when I retrieve the information from PHP it seems to remove the new lines and everything is on the same line.

Below is the code I am using to store the file in the database.

using (ConnectDb db = new ConnectDb(appSettings))
                {
                    FileStream fileStream = new FileStream(logFile, FileMode.Open, FileAccess.Read);
                    long fileLength = fileStream.Length;
                    byte[] myData = new byte[fileLength];
                    fileStream.Read(myData, 0, System.Convert.ToInt32(fileLength));
                    fileStream.Close();

                    string fileSize = Common.convertBytesToMb(fileLength);
                    string query = "INSERT INTO logfile_history (LogFile, FileSize) VALUES (@file, @size)";
                    using (MySqlCommand cmd = new MySqlCommand(query, db.conn))
                    {
                        cmd.Parameters.AddWithValue("@file", myData);
                        cmd.Parameters.AddWithValue("@size", fileSize);
                        cmd.ExecuteNonQuery();
                    }
                }

When I get the file from PHP the new lines are removed, below is the code I am using to get the data from the Medium Blob in the database from PHP.

$query = "SELECT * FROM logfile_history WHERE id='$id'";

    $result = mysql_query($query);
    if ($result)
    {
        echo "<table class=\"overview\">";
        echo "<tr>";
        echo "<th>Date</th>";
        echo "<th>Class/Method</th>";
        echo "<th>Message</th>";
        echo "</tr>";
        while ($myrow = mysql_fetch_array($result))
        {
            $logContent = $myrow['LogFile'];
            $splitContent = explode("\t", $logContent);
            $i = 0;
            $colCount = 0;
            while ($i < count($splitContent))
            {
                if ($colCount == 0)
                {
                    echo "<tr>";
                }
                echo "<td>" . $splitContent[$i] . "</td>";
                if ($colCount == 2)
                {
                    echo "</tr>";
                    $colCount = 0;
                }
                $i++;
                $colCount++;
            }
        }
        echo "</table>";

I’m splitting the line as in the log file it is tab separated so I am splitting it by the tab to put it in a table, but this was before I realised everything is on the same line.

How do I keep the line breaks when the data is retrieved from the database.

Thanks for any help you can provide.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:25:05+00:00Added an answer on June 9, 2026 at 4:25 pm

    Okay, I’m imagining that you have a log file that looks like this:

    Timestamp1    Method1    Message1
    Timestamp2    Method2    Message2
    Timestamp3    Method3    Message3
    

    …where 4 spaces in the above is a tab.

    This is multiple lines, but one string, which uses 1 row in the database. What you’re doing when you call PHP’s explode method is splitting the string on the tabs. So it looks like:

    echo $splitcontent
    #prints a list like ["Timestamp1", "Method1", "Message1\nTimestamp2", "Method2", "Message2\nTimestamp3","Method3", "Message3"]
    

    (If the syntax there is bad, it’s because I don’t actually know PHP.)

    The line breaks are preserved, but notice how the messages and timestamps get squished together in the same list element.

    You’ve ended up wrapping this in some HTML tags. I’m imagining that your HTML looks a bit like this:

    <table class="overview">
    <tr>
        <th>Date</th>
        <th>Class/Method</th>
        <th>Message</th>
    </tr>
    <tr>
        <td>Timestamp1</td>
        <td>Method1</td>
        <td>Message1\nTimestamp2</td>
    </tr>
    <tr>
        <td>Method2</td>
        <td>Message2\nTimestamp3</td>
        <td>Method3</td>
    </tr>
    <tr>
        <td>Message3</td>
    </table>
    

    (Obviously, this is the formatted version of the HTML.)

    HTML doesn’t recognize those line breaks as anything other than whitespace, obviously. And the formatting for your table is messed up.

    Anyway, the way you need to resolve this is that after your retrieve the string from the database, you need to split it into multiple lines. I think the source of some of your confusion here is that you seem to be thinking that a row in the database corresponds to a line in your text file. It doesn’t. You have a text file with multiple lines, which is stored as one row in the logs. As such, you don’t need to be iterating over the rows in the database results (that is, if your ID column is a primary key, which it probably is). Instead you should write something like:

    if ($logtext = mysql_fetch_array($result))
    {
        $loglines = explode("\n", $logtext)
        foreach($loglines as $line)
        {
            echo "<tr>"
            #explode on the tab characters and format each entry inside <td> tags
            echo "</tr>"
        }
    }
    

    Again, I don’t know PHP, so apologies if there are syntactic errors.

    Bottom line: you need to handle the line breaks in the file when you’re trying to format it for HTML, and the database will not help you format line breaks. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a (database-ish) project, where data is stored in a flat
I am working on a project that must store very large datasets and associated
I'm working on a project where I store an id in a table, which
I'm working on a project of which I should store constant multi dimensional arrays.I
I am working on a php project that needs to store information about various
While working a project tonight, I ended up using one .js resource file for
I am working on a project and i am not having any database. I
I am working on a project to make a database of the files I
Currently working on database part of android project. The main aim of the project
I'm working on a little project, the aim being to generate a report from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.