qwick0 Posted February 17, 2007 Share Posted February 17, 2007 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 More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 ya its cool but whats the error Link to comment https://forums.phpfreaks.com/topic/38965-check-if-record-exists-in-mysql/#findComment-187522 Share on other sites More sharing options...
JasonLewis Posted February 17, 2007 Share Posted February 17, 2007 is the $num variable being echoed? Link to comment https://forums.phpfreaks.com/topic/38965-check-if-record-exists-in-mysql/#findComment-187532 Share on other sites More sharing options...
qwick0 Posted February 18, 2007 Author Share Posted February 18, 2007 Oh the problem is that it does not work lol. I made that. Yes, $num is echoed. I wasnt sure if maybe there was a simple way to do a "loop until" type setup? Link to comment https://forums.phpfreaks.com/topic/38965-check-if-record-exists-in-mysql/#findComment-187640 Share on other sites More sharing options...
qwick0 Posted February 18, 2007 Author Share Posted February 18, 2007 OK, i figured things out. I was making it waaaaay more complicated than it needed to be. I just made a while loop and said do while the song is repeated. SO that way it would keep selecting a new song until it couldnt find it already existing. Link to comment https://forums.phpfreaks.com/topic/38965-check-if-record-exists-in-mysql/#findComment-187715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.