Jump to content

Help checking if numbers are in a database


patstantwtf

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.