显示数据库内容? PHP/MySQL

发布于 2024-07-17 02:46:52 字数 217 浏览 3 评论 0原文

因此,我有一种聊天室类型的数据库,其中用户插入的文本被存储到数据库中,作为他们的用户名在一个字段中,他们的消息在另一个字段中。 我想让我的页面输出数据库信息,以便人们可以看到彼此的消息。 我该怎么做呢?

另外,是否可以创建一个 for 循环来检查数据库是否已使用新消息进行更新,从而重新加载页面? (然后页面再次输出数据库信息以更新每个人的消息)

请帮忙..我很困惑。

So I have a chatroom type of database where the text that a user inserts gets stored into a databse as their username in one field and their message in the other. I want to have my page output the database info, so that people can see each others messages.
How do I do this?

Also, is it possible to make a for loop that checks to see if the database has been updated with a new message, therefore it reloads the page? (Then the page outputs the database info again to update everyones messages)

Please help.. i'm so confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-07-24 02:46:52

查看 PHP 手册中的 MySQL 函数。 您需要连接到服务器/数据库并运行选择查询以从表中获取数据。

至于循环:您可以使用 JavaScript setInterval 函数并将其与 AJAX 调用结合起来定期轮询新记录。

Take a look at MySQL functions in PHP manual. You need to connect to the server/database and run a select query to get the data from tables.

As for the loop: you could use JavaScript setInterval function and combine that with AJAX call to periodically poll for new records.

游魂 2024-07-24 02:46:52

就像其他人所说的那样,您将需要连接到数据库,然后查询包含数据的表。

while($row = mysql_fetch_assoc($results))
{
    echo $row['username'] . " said: " . $row['message'] . "<br />";
}

我使用 mysql_fetch_assoc() 而不是 mysql_fetch_array() 因为数组是关联数组(不是按整数索引,而是按索引)通过名称(关联))

至于在页面上动态显示更新,这就涉及到AJAX。 基本上,这意味着您的页面将调用后台脚本以从数据库中获取新记录。 这需要在“messages”表中添加一个新字段,例如“msg_delivered”,在获取该字段后您可以将其设置为“1”。

如果您有兴趣制作 AJAX 聊天客户端,您应该查看此内容: http: //htmltimes.com/javascript-chat-c​​lient-in-jquery.php

Like the others have said, you will want to connect to your database and then query the table that you have the data in.

while($row = mysql_fetch_assoc($results))
{
    echo $row['username'] . " said: " . $row['message'] . "<br />";
}

I use mysql_fetch_assoc() instead of mysql_fetch_array() since the arrays are associative arrays (not indexed by integers, but rather by names (associations))

As for displaying the update on the page dynamically, that involves AJAX. Basically what that means is that your page will call out to a background script to get the new records from the database. This would require a new field in your 'messages' table, something like 'msg_delivered' that you could set to '1' when it has been fetched.

You should check out this if you are interested in making an AJAX chat client: http://htmltimes.com/javascript-chat-client-in-jquery.php

一口甜 2024-07-24 02:46:52
  1. 要从 mysql 数据库读取任何内容,您可以使用 mysql_connect() 和 mysql_query() 函数
    例如:
$link = mysql_connect('localhost', 'root', ''); 
  $results = mysql_query('从消息中选择*'); 

  while($row = mysql_fetch_array($results)) 
  { 
      回显 $row['用户名'] 。   ':'。   $row['消息'].'
'; }
  1. 要显示新消息,最好的方法是使用 AJAX 并从那里轮询数据库,或者将单独的页面加载到 DIV 中,或者获取 XML 并将其放入 HTML 标记中。 我建议使用 JQuery 来完成此类任务。 检查 http://www.sitepoint.com/article/ajax-jquery/举个例子。
  1. To read anything from a mysql database you would use the mysql_connect() and the mysql_query() functions
    eg:
$link = mysql_connect('localhost', 'root', '');
$results = mysql_query('select * from messages');

while($row = mysql_fetch_array($results))
{
    echo $row['username'] . ': ' . $row['message'].'<br />';
}
  1. To display new messages the best way would be to use AJAX and poll the database from there, either loading a separate page into a DIV or getting XML back and placing into HTML tags. I would recommend using JQuery for these kinds of tasks. Check http://www.sitepoint.com/article/ajax-jquery/ for an example.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文