CKeditor,从数据库中检索用户文本
CKeditor, retrieve user text from database
目前我正忙于为 Web 应用程序制作一个简单的 CKeditor 记事本。我已经有了将用户文本保存到数据库中的代码。
现在我想添加将从数据库中检索最新保存的(最新 ID)文本的代码,以便用户可以继续 his/her 工作。
<?php
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')");
if($query)
echo "Succes!";
else
echo "Failed!";
}
?>
这是保存用户文本的代码。
现在我想构建代码来从数据库中检索最新保存的文本,但我无法开始使用我的代码。
<textarea name="editor1" id="editor1" rows="10" cols="80">
<?php
$conn = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$db") or die("ERROR");
$sql = "SELECT content from content";
?>
</textarea>
这是我目前拥有的。
您需要使用 mysqli_query()
and also need to fetch data by using mysqli_fetch_assoc()
来执行您的查询:
示例:
<textarea name="editor1" id="editor1" rows="10" cols="80">
<?php
$sql = "SELECT `content` FROM `content`";
$query = mysqli_query($conn,$sql);
$result = mysqli_fetch_assoc($query);
echo $result['content']; // will print your content.
?>
</textarea>
更新 1:
要获取最新记录,您可以在查询中使用 ORDER BY
和 LIMIT 1
作为:
$sql = "SELECT `content` FROM `content` ORDER BY id DESC LIMIT 1"; // assuming id is your primary key column.
目前我正忙于为 Web 应用程序制作一个简单的 CKeditor 记事本。我已经有了将用户文本保存到数据库中的代码。
现在我想添加将从数据库中检索最新保存的(最新 ID)文本的代码,以便用户可以继续 his/her 工作。
<?php
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')");
if($query)
echo "Succes!";
else
echo "Failed!";
}
?>
这是保存用户文本的代码。
现在我想构建代码来从数据库中检索最新保存的文本,但我无法开始使用我的代码。
<textarea name="editor1" id="editor1" rows="10" cols="80">
<?php
$conn = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$db") or die("ERROR");
$sql = "SELECT content from content";
?>
</textarea>
这是我目前拥有的。
您需要使用 mysqli_query()
and also need to fetch data by using mysqli_fetch_assoc()
来执行您的查询:
示例:
<textarea name="editor1" id="editor1" rows="10" cols="80">
<?php
$sql = "SELECT `content` FROM `content`";
$query = mysqli_query($conn,$sql);
$result = mysqli_fetch_assoc($query);
echo $result['content']; // will print your content.
?>
</textarea>
更新 1:
要获取最新记录,您可以在查询中使用 ORDER BY
和 LIMIT 1
作为:
$sql = "SELECT `content` FROM `content` ORDER BY id DESC LIMIT 1"; // assuming id is your primary key column.