简体   繁体   English

如何将更改的数据存储到数据库中?

[英]How do I store changing data into a database?

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 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. 输出只是一个php'echo $ variable'命令,但是$ variable不断变化,因此如何将$ variable的每个不同值存储到数据库中。 The end plan to to keep a list of all the songs that play over a certain period of time. 最终计划保留一段时间内播放的所有歌曲的列表。

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. 您必须将php脚本设置为每x分钟左右运行一次,而不是回显结果,而是对照当前播放的歌曲检查最新的数据库条目,如果不一致,则添加新记录。

First, some SQL to set up a new table: 首先,一些SQL来建立一个新表:

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脚本以在数据库中插入记录,而不是回显屏幕:

<?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. 设置计划任务或cron作业以每分钟运行php -f z1035.php

To see the entire list of songs, create a new php file station_history.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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM