如何使用PHP从MySql数据库中获取数据

时间:2015-07-01 06:02:10

标签: php mysql database xampp

我正在尝试使用来自Server的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();
?>

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

1 个答案:

答案 0 :(得分:1)

您可以使用nl2br将新换行符转换为<br>,这样无论您何时回复,只需调用nl2br函数,请参阅下面的示例:

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

修改

要获取空格而不是<br>,您可以简单地用空格替换换行符\n,或者您要替换的任何内容,请参阅下面的示例:

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

编辑2:

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