tutorials:other:online_playlist_on_your_website_with_php_mysql
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tutorials:other:online_playlist_on_your_website_with_php_mysql [2014/08/14 09:06] – created tw | tutorials:other:online_playlist_on_your_website_with_php_mysql [2021/06/18 06:40] (current) – Version ohne Datenbank tw | ||
---|---|---|---|
Line 8: | Line 8: | ||
====== Architecture ====== | ====== Architecture ====== | ||
+ | |||
+ | Our solution works as follows: | ||
+ | |||
+ | * A list of all played songs (start time, artist, title) is kept in a MySQL table. | ||
+ | * We place a " | ||
+ | * mAirList uses HTTP GET logging to transfer the song information whenever you start a new song. | ||
+ | * When the user visits your website, the PHP script receives the current item from the table. | ||
+ | ====== Setup ====== | ||
+ | |||
+ | ===== Preparing the MySQL Server ===== | ||
+ | |||
+ | We assume that you already have a MySQL server running, and that PHP is able to access it. For a tutorial, see e.g. here: [http:// | ||
+ | |||
+ | We have to create a new table, e.g. " | ||
+ | |||
+ | * The date/time at which the song was started | ||
+ | * The name of the artist | ||
+ | * The title of the song | ||
+ | |||
+ | You can add more fields if you like, and use mAirList Attributes to populate them. | ||
+ | |||
+ | It is also a good idea to create an [[https:// | ||
+ | |||
+ | To create the table and the index, open the MySQL console (or use a graphical frontend like phpMyAdmin) and issue the following commands: | ||
+ | |||
+ | <code sql> | ||
+ | CREATE TABLE playlistlog ( | ||
+ | starttime DATETIME NOT NULL, | ||
+ | artist VARCHAR(255), | ||
+ | title VARCHAR(255) | ||
+ | ); | ||
+ | CREATE INDEX playlistlog_starttime_index ON playlistlog(starttime); | ||
+ | </ | ||
+ | ===== Preparing PHP Receive Script ===== | ||
+ | |||
+ | Step #2 is to create a PHP script that receives new logging entries from mAirList via HTTP GET, and saves them into the MySQL table. | ||
+ | |||
+ | The script should be " | ||
+ | |||
+ | Let's assume the script is called '' | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | $db = mysqli_connect(' | ||
+ | |||
+ | $artist = mysqli_escape_string($db, | ||
+ | $title = mysqli_escape_string($db, | ||
+ | |||
+ | mysqli_query($db, | ||
+ | </ | ||
+ | |||
+ | Don't forget to adjust the MySQL login data in the first line ('' | ||
+ | ===== Setting up mAirList HTTP Logging ===== | ||
+ | |||
+ | We use HTTP GET to transfer new entries to the webserver, so go to //mAirList Configuration -> Logging//, click '' | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | As Base URL, enter the URL of the secret playlistlog.php script, e.g. http:// | ||
+ | |||
+ | In the Parameters table, add two parameters: //artist// with value //%a//, and //title// with value //%b//. **Use the cursor-down key to add more lines to the table.** | ||
+ | |||
+ | That's pretty much it. If you protected your script with an '' | ||
+ | |||
+ | Now run mAirList, start a couple of songs and watch how the '' | ||
+ | ===== Retrieving the Data ===== | ||
+ | |||
+ | Once the data is in the '' | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | $db = mysqli_connect(' | ||
+ | |||
+ | $res = mysqli_query($db, | ||
+ | |||
+ | print "< | ||
+ | while (list ($starttime, | ||
+ | print "< | ||
+ | } | ||
+ | mysqli_free_result($res); | ||
+ | print "</ | ||
+ | </ | ||
+ | |||
+ | As all entries are stored along with the date/time the song was started, it is also possible to implement a search function that looks up the songs played in a given time frame. | ||
+ | |||
+ | ====== Alternative Version without MySQL ====== | ||
+ | |||
+ | Here's a simple alternative version of the approach that does not use a database but only stores the current artist/ | ||
+ | |||
+ | inject.php: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | $handle = fopen('/ | ||
+ | fwrite($handle, | ||
+ | fwrite($handle, | ||
+ | fclose($handle); | ||
+ | |||
+ | print " | ||
+ | </ | ||
+ | |||
+ | nowplaying.php: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | $handle = fopen('/ | ||
+ | $artist = fgets($handle); | ||
+ | $title = fgets($handle); | ||
+ | fclose($handle); | ||
+ | |||
+ | print "Now playing: $artist - $title"; | ||
+ | </ | ||
tutorials/other/online_playlist_on_your_website_with_php_mysql.1408007209.txt.gz · Last modified: 2019/07/09 16:19 (external edit)