Missing newline characters while getting data from PostgreSQL database using PHP
P粉696146205
2023-08-06 13:44:42
<p>Title: Missing newline character when getting data from PostgreSQL database using PHP<br />Text:<br />I encountered a problem while getting data from PostgreSQL database using PHP. I'm using Navicat to view the data in my database and it clearly shows the newlines (n) in the data like this: </p><p><code></code>< /p>
<pre class="brush:php;toolbar:false;">addr=1
addr=2
addr=3
addr=4
addr=5
addr=6
j=6
[rs485] comid=1
comid=1
[rs485] baudrate=115200
bdrate=115200
...</pre>
<p>However, when I use PHP to get this data, the newlines seem to be lost. Here is the PHP code I am using to get the data: </p>
<pre class="lang-php prettyprint-override"><code>$host = '192.168.1.92';
$db = 'GneMaster';
$user = 'postgres';
$pass = '123456';
$dsn = "pgsql:host=$host;port=5432;dbname=$db;options='--client_encoding=UTF8'";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_STRINGIFY_FETCHES => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
$id = '648c0d5dae53ac34094ede6d';
$sql = "SELECT responsecontent FROM stationcmd WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_STR);
$stmt->execute();
while ($row = $stmt->fetch())
{
echo "Checking for line breaks: n";
$CRLFPos = strpos($row['responsecontent'], "rn");
$LFPos = strpos($row['responsecontent'], "n");
$CRPos = strpos($row['responsecontent'], "r");
if ($CRLFPos !== false) {
echo "CRLF found at position: " . $CRLFPos . "n";
}
if ($LFPos !== false) {
echo "LF found at position: " . $LFPos . "n";
}
if ($CRPos !== false) {
echo "CR found at position: " . $CRPos . "n";
}
if ($CRLFPos === false && $LFPos === false && $CRPos === false) {
echo "No line breaks found in responsecontentn";
}
}
</code></pre>
<p>The output of this PHP script does not contain any newlines, as shown below: </p>
<pre class="brush:php;toolbar:false;">addr=1addr=2addr=3addr=4addr=5addr=6j=6[rs485] comid=1comid=1[rs485] baudrate=115200bdrate=115200.. .</pre>
<p>Why are newlines lost when I get data using PHP, and how do I keep them? <br /><br />I would like to be able to preserve the original newlines when fetching data from a PostgreSQL database using PHP, as seen in Navicat.From what I understand, when I get a string from the database, it should preserve all characters, including newlines. <br /><br />The approach I tried was to get the data directly from the database and output it. However, the resulting output does not contain any newlines. I also tried looking for newline characters (n, r, rn) in PHP but these characters are not found in my data. <br /><br />What I expect is that when I get and output the data using PHP, it looks the same as in Navicat, i.e. the data should contain newlines. However, the actual result is that the data I receive does not contain newlines. <br /><br />I also checked some documentation about PHP and PostgreSQL, but found no information related to this problem. I searched on Stack Overflow and other related forums but found no similar problem or solution. <br /><br />I'm looking for a way to preserve the original newlines when fetching data in PHP. I have some knowledge of PHP and PostgreSQL but haven't found a solution to this problem. I hope someone can help me solve this problem. </p><p><br /></p>
I'm sorry for the confusion. The PHP code I provided connects to a PostgreSQL database at 192.168.1.92, but the database I'm viewing in Navicat is at 192.168.1.93. The database at 192.168.1.93 contains newlines, while the database at 192.168.1.92 does not. This difference caused the problem I encountered. I'm deeply sorry for any confusion caused.