我目前正在编写一个聊天系统,我已将消息输入我的数据库(MySql)所以我的问题是我不知道如何从我的数据库中获取这些消息并显示它。
<ol class="chat">
<li class="other">
<div class="message">
<p></p>
</div>
</li>
</ol>
我希望消息显示在
中
这是从数据库&#34;
获取消息的良好开端$query = "SELECT messages, sender FROM `messages`";
所有回复的Thx!
好的,这是我的完整PHP代码:
<?php
ob_start();
session_start();
include '../Datenbank/dbconnect.php';
// if session is not set this will redirect to login page
if (!isset($_SESSION['user'])) {
header("Location: ../login/index.php");
exit;
}
// select loggedin users detail
$res = mysql_query("SELECT * FROM users WHERE userId=" . $_SESSION['user']);
$userRow = mysql_fetch_array($res);
$error = false;
$sender = $userRow['userName'];
$conn = mysql_connect('localhost', 'root', '', 'dbtest');
$query = "SELECT messages, sender, timestamp FROM `messages` ORDER BY `timestamp`";
$result = mysql_query($conn ,$query);
echo '<ol class="chat">';
while ($res = mysql_fetch_assoc($result)) {
echo "
<li class='other'>
<div class='message'>
<p>Message: ".$res['messages']."</p>
<p>Sender: ".$res['sender']."</p>
<p>Time: ".$res['timestamp']."</p>
</div>
</li>";
}
echo '</ol>';
mysql_close($conn);
if (isset($_POST['send'])) {
$test = true;
$message = $_POST['message'];
if (empty($message)) {
$error = true;
$messageError = "Please enter your message.";
}
if (!$error) {
$query = "INSERT INTO messages(message,sender) VALUES('$message','$sender')";
$res = mysql_query($query);
if ($res) {
unset($message);
} else {
$errTyp2 = "danger";
$errMSG2 = "Something went wrong, try again later...";
}
}
};
?>
Dbconnect.php:
<?php
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'dbtest');
$conn = mysql_connect(DBHOST,DBUSER,DBPASS);
$dbcon = mysql_select_db(DBNAME);
if ( !$conn ) {
die("Connection failed : " . mysql_error());
}
if ( !$dbcon ) {
die("Database Connection failed : " . mysql_error());
}
答案 0 :(得分:0)
您使用的是哪种PHP SQL方法? mysql_connect,mysqli_connect还是PDO? 我将举例说明mysqli和PDO,因为不推荐使用mysql_connect。
MySQLi示例
Set-ConsoleFont 10
PDO示例
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
// sql your query
$sql = "SELECT messages, sender FROM messages";
$result = mysqli_query($conn, $sql);
echo '<ol class="chat">';
while($row = mysqli_fetch_assoc($result)) {
{
echo "
<li class='other'>
<div class='message'>
<p>Message: ".$row['messages']."</p> <!-- output message -->
<p>Sender: ".$row['sender']."</p> <!-- output sender name -->
</div>
</li>";
}
echo '</ol>';
mysqli_close($conn);
Mysql_connect示例(警告!此方法在PHP 7中已弃用)
try
{
// create connection
$conn = new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT messages, sender FROM messages"); // your sql query
$stmt->execute();
echo '<ol class="chat">';
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "
<li class='other'>
<div class='message'>
<p>Message: ".$row['messages']."</p> <!-- output message -->
<p>Sender: ".$row['sender']."</p> <!-- output sender name -->
</div>
</li>";
}
echo "</ol>";
}
catch(PDOException $e)
{
echo "Connection Error : " . $e->getMessage();
}
$conn = null;