dpalame Posted August 8, 2011 Share Posted August 8, 2011 Hello, I have a select statement that works great: "SELECT *, ( 3959 * acos( cos( radians($lat) ) * cos( radians( loc_LAT_centroid ) ) * cos( radians( loc_LONG_centroid ) - radians($lon) ) + sin( radians($lat) ) * sin( radians( loc_LAT_centroid ) ) ) ) AS distance FROM allStores HAVING distance <= '$miles' ORDER BY distance" I want to add one more variable to check in the table. Something like WHERE name LIKE '%$name%' Can't figure out where to put it in the select statement. Any help would be appreciated. Thank you. Damon Quote Link to comment https://forums.phpfreaks.com/topic/244244-php-select-statement-mysql-syntax/ Share on other sites More sharing options...
AyKay47 Posted August 8, 2011 Share Posted August 8, 2011 "SELECT *, ( 3959 * acos( cos( radians($lat) ) * cos( radians( loc_LAT_centroid ) ) * cos( radians( loc_LONG_centroid ) - radians($lon) ) + sin( radians($lat) ) * sin( radians( loc_LAT_centroid ) ) ) ) FROM allStores AS distance WHERE distance <= '$miles' AND name LIKE "%$name%" ORDER BY distance" Quote Link to comment https://forums.phpfreaks.com/topic/244244-php-select-statement-mysql-syntax/#findComment-1254459 Share on other sites More sharing options...
dpalame Posted August 8, 2011 Author Share Posted August 8, 2011 Thank you but that code is not returning any rows. The $name variable is an array. Is that what is messing it up? Quote Link to comment https://forums.phpfreaks.com/topic/244244-php-select-statement-mysql-syntax/#findComment-1254475 Share on other sites More sharing options...
dpalame Posted August 8, 2011 Author Share Posted August 8, 2011 I tried the code without the extra variable and it doesn't work: "SELECT *, ( 3959 * acos( cos( radians($lat) ) * cos( radians( loc_LAT_centroid ) ) * cos( radians( loc_LONG_centroid ) - radians($lon) ) + sin( radians($lat) ) * sin( radians( loc_LAT_centroid ) ) ) ) FROM allStores AS distance WHERE distance <= '$miles' ORDER BY distance" Quote Link to comment https://forums.phpfreaks.com/topic/244244-php-select-statement-mysql-syntax/#findComment-1254479 Share on other sites More sharing options...
TeNDoLLA Posted August 8, 2011 Share Posted August 8, 2011 Apparently you can't use an array as value for SQL. You could create the extra part for the sql query manually and then add it to the SQL-clause. e.g. $name = array('test', 'test2', 'test3'); $sqlExtra = "AND ("; foreach ($name as $val) { $sqlExtra .= "name LIKE '%$val%' OR "; } $sqlExtra = substr($sqlExtra, 0, -4) . ')'; var_dump($sqlExtra); // Outputs AND (name LIKE '%test%' OR name LIKE '%test2%' OR name LIKE '%test3%') $sql = "SELECT *, ( 3959 * acos( cos( radians($lat) ) * cos( radians( loc_LAT_centroid ) ) * cos( radians( loc_LONG_centroid ) - radians($lon) ) + sin( radians($lat) ) * sin( radians( loc_LAT_centroid ) ) ) ) FROM allStores AS distance WHERE distance <= '$miles' $sqlExtra ORDER BY distance" Quote Link to comment https://forums.phpfreaks.com/topic/244244-php-select-statement-mysql-syntax/#findComment-1254480 Share on other sites More sharing options...
dpalame Posted August 8, 2011 Author Share Posted August 8, 2011 Thanks everyone for your help. Used the following syntax and it works great: $sqlExtra = "("; foreach ($at2 as $val) { $sqlExtra .= "cat_primary LIKE '%$val%' OR "; } $sqlExtra = substr($sqlExtra, 0, -4) . ')'; // var_dump($sqlExtra); // Outputs AND (name LIKE '%test%' OR name LIKE '%test2%' OR name LIKE '%test3%') $query = "SELECT *, ( 3959 * acos( cos( radians($lat) ) * cos( radians( loc_LAT_centroid ) ) * cos( radians( loc_LONG_centroid ) - radians($lon) ) + sin( radians($lat) ) * sin( radians( loc_LAT_centroid ) ) ) ) AS distance FROM allStores WHERE $sqlExtra HAVING distance <= '$miles' ORDER BY distance"; Quote Link to comment https://forums.phpfreaks.com/topic/244244-php-select-statement-mysql-syntax/#findComment-1254507 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.