Jump to content

Check if record exists in mysql


qwick0

Recommended Posts

Ok, This seems like it should be simple, but I cannot for the life of me figure it out.  What I am doing is selecting a random value from a mysql table, and then inserting that value into another table.  However, I need to check to make sure the random value i pulled has not already been used.  I can only randomly select a Value 1 time.  This is the code i put together, but its messy and doesnt work lol.  There are a few random echo's in there, but thats just because i was trying to track what was going on.  Ignore them.

 

function select_song(){
$squery= "Select Song FROM Songs WHERE Final=0 ORDER BY rand()";
$sresult = mysql_query($squery) or die (mysql_error());
$srow = mysql_fetch_array($sresult, MYSQL_ASSOC);
$song=$srow["Song"];
if (song_check($song)){
	select_song2();
	return $song;}
	else{
	return $song;}
}

function song_check($song){
 $query = "SELECT Song FROM Expert WHERE Song = '$song'";
 $result = mysql_query($query) or die (mysql_error());
 $num = mysql_num_rows($result);
	if ($num > 0){
	echo "<br>$num<br>";
	return TRUE;
 	}else{
 	return FALSE;
 	}
  }

Link to comment
https://forums.phpfreaks.com/topic/38965-check-if-record-exists-in-mysql/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.