Jump to content

Recommended Posts

I am trying to build a shoutcast radio logger. I have it down and working, only my table looks like this 2HDFsYM.png
I want to have a check to compare the last song to the current song as to not record the same song twice as it is still playing.
 
My table is made as such 

CREATE TABLE  `db`.`table` (
`pId` INT( 11 ) NOT NULL AUTO_INCREMENT ,
 `SongTitle` CHAR( 255 ) DEFAULT NULL ,
 `playedDate` DATE DEFAULT NULL ,
 `playedTime` TIME DEFAULT NULL ,
PRIMARY KEY (  `pId` )
)

My php script as such

<?php 
/*Date and Time*/
$datetime = new DateTime();
$timestamp = $datetime->format('H:i:s');
$datestamp = $datetime->format('Y-m-d');
/*Get song*/
$song = file_get_contents('http://radio.ponyvillelive.com:8008/currentsong?sid=1');
/*SQL Connect*/
$con = mysqli_connect("localhost","root","password","db");
/*Get the highest pID*/
$maxpId = mysqli_query($con,"SELECT MAX(pId) FROM `table`");
$maxpId = mysqli_fetch_array($maxpId, MYSQL_BOTH);
$maxpId = $maxpId[0];
/*Run query*/
mysqli_query($con,"INSERT INTO table (songtitle, playeddate, playedtime)
VALUES ('$song','$datestamp','$timestamp')");
/*Close Connection*/
mysql_close($con);
/*Echo variables*/
echo 'Song: '."$song".'<br>Date: '."$datestamp".'<br>Time: '."$timestamp".'<br>pId: '."$maxpId";
?>

I have looked at this http://forums.phpfreaks.com/topic/221701-compare-mysql-row-value-to-php-variable/ and tried using it however it seems I am missing something.

 

Basicly take the highest pId ($maxpId) from the table, comapare $song to that row's songtitle, then

if same {echo "Song is already in for this play. Nothing recorded";} else {echo "Song was recorded."; mysqli_query($con,"INSERT INTO table (songtitle, playeddate, playedtime) VALUES ('$song','$datestamp','$timestamp')");}
Link to comment
https://forums.phpfreaks.com/topic/282420-mysqli-php-compare-variable-to-sql-row/
Share on other sites

You'll want to modify the SongTitle database field so its a unique/primary key. Then you can change your INSERT query to an INSERT IGNORE query, MySQL will then ignore any duplicate entries with the same song title

 

This may help you further.

http://www.tutorialspoint.com/mysql/mysql-handling-duplicates.htm

Edited by Ch0cu3r

You'll want to modify the SongTitle database field so its a unique/primary key. ...

That would certainly prevent "record(ing) the same song twice "; however, I suspect the OP means "same song twice in a row".

 

You need to get the title of the last song recorded:

SELECT SongTitle FROM `table` WHERE pId = (SELECT MAX(pId) FROM `table`)
However, I would not recommend using the pId for this. I would combine the playedDate and playedTime into a single DATETIME field, and use playedDateTime = (SELECT MAX(playedDateTime).

 

Furthermore, if this is a continuously running job, I would just save the "last song" in a PHP variable for immediate comparison instead of going back to the database repeatedly. To prevent duplicates when the script starts (say after a reboot or whatever), load the last song from the database at startup.

 

Oh, and give your table a meaningful name. "Table" is just not helpful, and creates the need to use the backticks whenever you query it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.