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.
Okay, I’m imagining that you have a log file that looks like this:
…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:
(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:
(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:
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. 😉