NYCPHP Meetup

NYPHP.org

[nycphp-talk] Mysql Selects

Adam Fields fields at hedge.net
Tue Jun 21 17:06:04 EDT 2005


On Tue, Jun 21, 2005 at 08:48:13PM +0000, drydell at att.net wrote:
> IN is a standard construct in every DB engine I can think of... IN expects a comma delimited list:
> 
> SELECT cols FROM table WHERE zip in (11111, 22222, 33333)
> 
> 
> with PHP, you can do something like:
> 
> $zips = array(11111, 22222, 33333);
> $rs = mysql_query('SELECT cols FROM table WHERE zip in ('.join(',', $zips).')');

When building sql queries dynamically, I find it very useful to store
the query in a variable first:

$sql = 'SELECT cols FROM table WHERE zip in ('.join(',', $zips).')';
$rs = mysql_query($sql);

If you have problems with the query, you can easily print it.

This also helps when you ultimately decide you want a database wrapper
and you're replacing mysql_query with something else - you can just do
a global search and replace for "mysql_query($sql)". :)


-- 
				- Adam

** I can fix your database problems: http://www.everylastounce.com/mysql.html **

Blog............... [ http://www.aquick.org/blog ]
Links.............. [ http://del.icio.us/fields ]
Photos............. [ http://www.aquick.org/photoblog ]
Experience......... [ http://www.adamfields.com/resume.html ]
Product Reviews: .. [ http://www.buyadam.com/blog ]




More information about the talk mailing list