如何将变化的数据存储到数据库中?

发布于 2024-07-30 08:11:54 字数 280 浏览 4 评论 0原文

我制作了一个应用程序,从互联网广播流中获取正在播放的标题和艺术家,并在此页面上以纯文本形式显示它:

http://thelisthq.net/z1035.php

输出只是一个 php 'echo $variable' 命令,但是 $variable 不断变化,那么如何存储 $variable 的每个不同值到数据库中。 最终计划保留一段时间内播放的所有歌曲的列表。

I made an application to take the now playing title and artist from an internet radio stream, and I have it displaying in plain text on this page:

http://thelisthq.net/z1035.php

The output is simply a php 'echo $variable' command, but $variable is constantly changing, so how do I store each different value of $variable into a database. The end plan to to keep a list of all the songs that play over a certain period of time.

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

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

发布评论

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

评论(1

浊酒尽余欢 2024-08-06 08:11:54

您必须将 php 脚本设置为每 x 分钟左右运行一次,而不是回显结果,而是根据当前播放的歌曲检查最新的数据库条目,如果不同,则添加新记录。

首先,使用一些 SQL 来设置新表:

CREATE TABLE SongList (
    ID int primary key auto_increment not null,
    Song char(255) not null,
    PlayTime Datetime not null
);

然后,修改 PHP 脚本以在数据库中插入记录,而不是回显到屏幕:

<?php
$song = get_song(); //not sure how you're doing that
$sql = "SELECT Song FROM SongList ORDER BY PlayTime DESC LIMIT 1";
list($prevSong) = mysql_fetch_row(mysql_query($sql));
if ($song !== $prevSong) {
    $sql = "INSERT INTO SongList (Song, PlayTime) VALUES ('$song', NOW())";
    mysql_query($sql);
}
?>

设置计划任务或 cron 作业来运行 php -f z1035.php 每分钟。

要查看完整的歌曲列表,请创建一个新的 php 文件 station_history.php

<html>
<body>
<pre>
<?php
$sql = "SELECT Song, PlayTime FROM SongList LIMIT 20"; // Just the last 20 songs
$result = mysql_query($sql);
while(list($song, $playtime) = mysql_fetch_row($result)) {
    echo "[$playtime] $song\n";
}
?>
</pre>
</body>
</html>

You'll have to set up the php script to run every x minutes or so, and instead of echoing the result, check the most recent database entry against the currently playing song, and if it is different, add a new record.

First, some SQL to set up a new table:

CREATE TABLE SongList (
    ID int primary key auto_increment not null,
    Song char(255) not null,
    PlayTime Datetime not null
);

Then, modify your PHP script to insert records in the database instead of echoing to the screen:

<?php
$song = get_song(); //not sure how you're doing that
$sql = "SELECT Song FROM SongList ORDER BY PlayTime DESC LIMIT 1";
list($prevSong) = mysql_fetch_row(mysql_query($sql));
if ($song !== $prevSong) {
    $sql = "INSERT INTO SongList (Song, PlayTime) VALUES ('$song', NOW())";
    mysql_query($sql);
}
?>

Set up a scheduled task or a cron job to run php -f z1035.php every minute.

To see the entire list of songs, create a new php file station_history.php

<html>
<body>
<pre>
<?php
$sql = "SELECT Song, PlayTime FROM SongList LIMIT 20"; // Just the last 20 songs
$result = mysql_query($sql);
while(list($song, $playtime) = mysql_fetch_row($result)) {
    echo "[$playtime] $song\n";
}
?>
</pre>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文