Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/244244-php-select-statement-mysql-syntax/
Share on other sites

"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"

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"

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"

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";

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.