【问题标题】:How to fetch data as it is from MySql Database using PHP如何使用 PHP 从 MySql 数据库中获取数据
【发布时间】:2026-01-24 09:15:02
【问题描述】:

我正在尝试使用服务器中的 PHP 脚本从 MySQL 数据库中获取数据。我能够从数据库中获取数据,但我没有得到数据库中存在的确切字符串。在获得的结果中,单词之间的空格被修剪,结果与数据库中存在的字符串不匹配。

例如:

插入到数据库中的值如下图所示:

SELENIUM INTERVIEW QUESTIONS:
What is Selenium?
Selenium is a set of tools that supports rapid development of test automation scripts for web based applications. Selenium testing tools provides a rich set of testing functions specifically designed to fulfill needs of testing of a web based application.
What are the main components of Selenium testing tools?
Selenium IDE, Selenium RC and Selenium Grid

从数据库查询获得的结果显示数据为:

SELENIUM INTERVIEW QUESTIONS:What is Selenium?Selenium is a set of tools that supports rapid development of test automation scripts for web basedapplications. Selenium testing tools provides a rich set of testing functions specifically designed to fulfill needs of testing of a web based application.What are the main components of Selenium testing tools?Selenium IDE, Selenium RC and Selenium Grid

谁能告诉我我应该在我的脚本中进行哪些更改才能从我的查询中获取数据库中显示的数据。我在插入时使用mysql_real_escape_String,在从数据库中检索数据时使用stripslashes

下面是我的 PHP 脚本:

插入脚本:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "iFocusBlogs";


        $obtainedName = urldecode($_POST['enteredName']);
       $obtainedUserName = urldecode($_POST['enteredUserName']);
       $obtainedsubjectText = urldecode($_POST['subjectText']);
       $obtaineddetailsText   = urldecode($_POST['detailsText']);
       $status  = urldecode($_POST['status']);         

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



$obtainedsubjectText = $conn->real_escape_string($obtainedsubjectText);
$obtaineddetailsText = $conn->real_escape_string($obtaineddetailsText);


$sql = "INSERT INTO AndroidTable (Name, UserName, Subject, Details, Status)
VALUES ('$obtainedName', '$obtainedUserName', '$obtainedsubjectText', '$obtaineddetailsText', '$status')";

mysqli_commit($conn);

if ($conn->query($sql) === TRUE) {
    echo "Inserted Post sent to Moderator for Approval. Once approved from Moderator, Post will be displayed";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

获取脚本:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "iFocusBlogs";



$obtainedUserName = 1;         

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


$sql="SELECT Name, Subject FROM AndroidTable WHERE Status ='" .$obtainedUserName. "'";
$result=mysqli_query($conn,$sql);


while ($row = mysqli_fetch_row($result)) {
foreach($row as $rows){
    for($i=0;$i<count($rows);$i++){
    echo  stripslashes($rows) . " ";
        $n=$i;

    }

}
    echo "<br/>";

    }


$conn->close();
?>

请让我知道我在脚本中犯了什么错误。欢迎所有建议。如果需要更多信息,请告诉我。提前致谢。

【问题讨论】:

  • 你的name栏有问题,Subject栏有对应的答案吗?
  • 我认为您的脚本只是返回数据库中的内容。在 HTML 中,换行符将被忽略,因为您必须将答案包装在
    ..
    标记中或用
    标记
    替换换行符
  • @anantkumarsingh 不,姓名是人名,主题是人发布的内容
  • @AndréSchild 是的,它显示,但它忽略了那些 sapces,我希望查询返回包含空格的数据。

标签: php mysql database xampp


【解决方案1】:

你可以使用nl2br,它将换行符转换为&lt;br&gt;,所以无论你在哪里回显,你只需要调用nl2br函数,见下例:

echo  nl2br(stripslashes($rows)) . " ";

编辑:

要获取空格而不是 &lt;br&gt;,您可以简单地将换行符 \n 替换为空格或您想替换的任何内容,请参见下面的示例:

echo str_replace("\n", " ", stripslashes($rows))

编辑 2:

echo stripslashes(str_replace(array('\r\n', '\n'), "<br>", $rows));

【讨论】:

  • 好的。我会尽力让你知道。
  • 是否有显示空格的功能,而不是
    我需要在 android 应用程序中显示此文本,所以如果我得到 sapces 而不是
    会很有帮助
  • 如果我在脚本中给出更新答案是否足够,我需要同时使用 nl2br 和更新答案对吗?
  • 我得到了
    ,但我不会在结果中得到 \n,所以我需要用空格替换 \n?
  • str_replace("\n", "&lt;br&gt;&amp;nbsp;", stripslashes($rows)) 这将用 br 标签和一个空格替换新行字符。这是你需要的吗?