简体   繁体   中英

How to fetch data as it is from MySql Database using PHP

I am trying to fetch Data from MySQL Database using PHP script from Server. I am able to get Data from Database, but I am not getting the exact string present in Database. In the result obtained the spaces between words get trimmed and result does not match with String present in Database.

For Example:

The value inserted to Database is as shown Below:

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

The result obtained from the Database query shows the data as:

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

Can any one please let me know what changes should I make in my script to obtain data as it is shown in database from my query. I am using mysql_real_escape_String while inserting and I am using stripslashes while retrieving data from database.

Below is my PHP script:

Insert Script:

<?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();
?>

fetch Script:

<?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();
?>

Please let me know what mistake am I doing in my script. All suggestions are welcome. If more information required please let me know. Thanks in advance.

You can use nl2br , which will convert new line characters to <br> , so wherever you are echoing, you just need to call nl2br function, see example below:

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

EDIT:

To get spaces instead of <br> , you can simply replace new line character \\n with space, or anything you would like to replace with, see example below:

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

EDIT 2:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM