MDanz Posted October 14, 2011 Share Posted October 14, 2011 I have a large array. Is there an alternate method to check if $value in the array is present in the MySql table vote and if not then insert $value into vote. This is what i am doing currently. Is there a better method? foreach($rowids as $value) { $select = mysql_query("SELECT voteid FROM vote WHERE username='$username' AND voteid='$value' LIMIT 1",$this->connect); if(mysql_num_rows($select)==0) { $insert = mysql_query("INSERT INTO vote VALUES ('','$value','$username')",$this->connect); } } Quote Link to comment https://forums.phpfreaks.com/topic/249092-alternative-to-foreach-mysql-help/ Share on other sites More sharing options...
fenway Posted October 14, 2011 Share Posted October 14, 2011 Why do you need to insert blank? Quote Link to comment https://forums.phpfreaks.com/topic/249092-alternative-to-foreach-mysql-help/#findComment-1279383 Share on other sites More sharing options...
MDanz Posted October 14, 2011 Author Share Posted October 14, 2011 blank is the auto incremented primary key id of the mysql table vote. Quote Link to comment https://forums.phpfreaks.com/topic/249092-alternative-to-foreach-mysql-help/#findComment-1279458 Share on other sites More sharing options...
fenway Posted October 14, 2011 Share Posted October 14, 2011 You should never do that. First, you should always explicitly list your column names. Two, you should leave out the auto-increment column. Three, why are you copying data? Quote Link to comment https://forums.phpfreaks.com/topic/249092-alternative-to-foreach-mysql-help/#findComment-1279475 Share on other sites More sharing options...
MDanz Posted October 14, 2011 Author Share Posted October 14, 2011 so you mean like this foreach($rowids as $value) { $select = mysql_query("SELECT voteid FROM vote WHERE username='$username' AND voteid='$value' LIMIT 1",$this->connect); if(mysql_num_rows($select)==0) { $insert = mysql_query("INSERT INTO vote (id,voteid,username) VALUES ('','$value','$username')",$this->connect); } } i still need another method rather than a foreach loop. Could unique index or insert ignore work? voteid is the id of a row from a different table btw. Quote Link to comment https://forums.phpfreaks.com/topic/249092-alternative-to-foreach-mysql-help/#findComment-1279477 Share on other sites More sharing options...
fenway Posted October 14, 2011 Share Posted October 14, 2011 And INSERT INTO... SELECT WHERE would work. But, again, why are you duplicating data? Quote Link to comment https://forums.phpfreaks.com/topic/249092-alternative-to-foreach-mysql-help/#findComment-1279486 Share on other sites More sharing options...
MDanz Posted October 14, 2011 Author Share Posted October 14, 2011 what data am i duplicating? $rowids is an array with different values. Could i have an example of this And INSERT INTO... SELECT WHERE would work. Quote Link to comment https://forums.phpfreaks.com/topic/249092-alternative-to-foreach-mysql-help/#findComment-1279491 Share on other sites More sharing options...
fenway Posted October 16, 2011 Share Posted October 16, 2011 You're copying value & username from table1 to table2. Quote Link to comment https://forums.phpfreaks.com/topic/249092-alternative-to-foreach-mysql-help/#findComment-1279731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.