【发布时间】: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,我希望查询返回包含空格的数据。