显示数据库内容? PHP/MySQL
因此,我有一种聊天室类型的数据库,其中用户插入的文本被存储到数据库中,作为他们的用户名在一个字段中,他们的消息在另一个字段中。 我想让我的页面输出数据库信息,以便人们可以看到彼此的消息。 我该怎么做呢?
另外,是否可以创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 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.
就像其他人所说的那样,您将需要连接到数据库,然后查询包含数据的表。
我使用 mysql_fetch_assoc() 而不是 mysql_fetch_array() 因为数组是关联数组(不是按整数索引,而是按索引)通过名称(关联))
至于在页面上动态显示更新,这就涉及到AJAX。 基本上,这意味着您的页面将调用后台脚本以从数据库中获取新记录。 这需要在“messages”表中添加一个新字段,例如“msg_delivered”,在获取该字段后您可以将其设置为“1”。
如果您有兴趣制作 AJAX 聊天客户端,您应该查看此内容: http: //htmltimes.com/javascript-chat-client-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.
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
例如:
mysql_connect()
and themysql_query()
functionseg: