SmashingJummy SmashingJummy - 2 years ago 145
PHP Question

CKeditor, retrieve user text from database

Currently I'm busy with a simple CKeditor notepad for webapplication. I already have code to save the user text into the database.

Now I want to add code that will retrieve the latest saved (latest id) text from the database, so the user can continue his/her work.

if(isset($_POST['editor1'])) {
$text = $_POST['editor1'];

$conn = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$db") or die("ERROR");

$query = mysqli_query($conn, "INSERT INTO content (content) VALUES ('$text')");

echo "Succes!";
echo "Failed!";

this is the code to save the users text.

Now I want to build code that will retrieve the latest saved text from the database, but I can't make a start with my code.

<textarea name="editor1" id="editor1" rows="10" cols="80">

$conn = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$db") or die("ERROR");

$sql = "SELECT content from content";


This is what I currently have.

Answer Source

You need to execute your query by using mysqli_query() and also need to fetch data by using mysqli_fetch_assoc() as:


<textarea name="editor1" id="editor1" rows="10" cols="80">
$sql = "SELECT `content` FROM `content`";
$query = mysqli_query($conn,$sql);
$result = mysqli_fetch_assoc($query);
echo $result['content']; // will print your content.


For fetching latest record than you can use ORDER BY in your query as:

$sql = "SELECT `content` FROM `content` ORDER BY id DESC"; // assuming id is your primary key column.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download