patstantwtf Posted October 19, 2011 Share Posted October 19, 2011 Hello! I need to loop through a list of numbers and check if they are present in a column in a database or not. I'm having a hard time figuring out how to check the column value by value and compare to a incrementing $i value. here is my query... $query2 = mysql_query("SELECT * FROM video"); $column = mysql_fetch_array($query2); echo $column2['cable_number']; this only echoes me the first ['cable_number'] in the database. i need to get every single one, and perform actions on each one as they come through. and here is my for() statement for($i=100;$i<=$highest;$i++){ check if $i is in the database. if it is(do something) if it isn't(do something) } i need to use that $i each iteration and see if it's present in the 'cable_number' column in the database. then i do something and continue on with the next i$ value. Quote Link to comment https://forums.phpfreaks.com/topic/249406-help-checking-if-numbers-are-in-a-database/ Share on other sites More sharing options...
premiso Posted October 19, 2011 Share Posted October 19, 2011 I would just create a range using the highest and then use that in an IN() statement in mysql, something like: $range = range(100, $highest); $query2 = mysql_query('SELECT * FROM video WHERE cable_number IN(' . implode(',', $range) . ')'); while ($row = mysql_fetch_assoc($query2)) { echo $row['cable_number'] . ' was in the range! Hooray!<br />'; } Which should be a bit more efficient then the other route. Not sure if this is what you were looking for, however. Quote Link to comment https://forums.phpfreaks.com/topic/249406-help-checking-if-numbers-are-in-a-database/#findComment-1280578 Share on other sites More sharing options...
requinix Posted October 19, 2011 Share Posted October 19, 2011 ...or WHERE cable_number BETWEEN 100 AND $highest Quote Link to comment https://forums.phpfreaks.com/topic/249406-help-checking-if-numbers-are-in-a-database/#findComment-1280586 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.