Daily Archives Thursday, March 2007

SQL: Selecting Specifying Multiple Field Values

Here are 3 different ways to perform a DB select when you want to specify two or more values for a field.
1. Use th OR operator
PLAIN TEXT
SQL:

SELECT * FROM people

WHERE (first_name = 'pete'

OR first_name = 'paul');

2. Use a Regular Expression
PLAIN TEXT
SQL:

SELECT * FROM people

WHERE first_name ~'^pete|paul$';

3. Use an array
PLAIN TEXT
SQL:

SELECT * FROM people

WHERE [...]

PHP: Remove Values from Array

If you need to remove elements from an array that have a particular value(s) here’s a neat way of doing it without any looping:
PLAIN TEXT
PHP:

// our initial array

$arr = Array("blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red");

print_r($arr);

 

// remove the elements who's values are yellow or red

$arr = array_diff($arr, array("yellow", "red"));

print_r($arr);

 

// optionally you could reindex [...]