Jump to content

I want to modify a query that display WHERE r.zip=$variable


co.ador

Recommended Posts

SELECT column1, column2, ...

  FROM daTable

WHERE columnx = 'foo'

 

The above query will only display the rows in columnx where the 'foo' string is found. And it will only display 'foo' NOT any other string.

 

Now the little monster query below will display and GROUP all the rows in clumnx where zipcode 10022 is found And GROUP it by first displaying all the rows in columnx that contain zipcode 10022 but as in the query above It will also continue displaying all the rows in columnx that contain a zipcode other than 10022 but that's after first displaying all the rows in columnx that zipcode is 10022

 

Now I want the query below to display zipcode 10022 only and only zipcode 10022 that would be nice. displaying other zipcode other than 10022 would be a possibility if I would be using latitude and longitude I guess, But I would also like to be able to display only zipcode 10022 only.

 

<?php
  if(!empty($strZipCode)) {
        $boolIncludeZipCodes = true;
    
        $arrSQLFilters[] = sprintf(
            "r.zip LIKE '%s'"
            ,"%$strZipCode%"
        );
    }

$strSQL = sprintf(
    'SELECT
         r.restaurants_id
        ,r.restaurantname
        ,r.image
        ,r.description
        ,r.address
        ,r.zip
        ,r.state
    FROM
        restaurants r
        %s
        %s
        %s
        LIMIT %d, %d
'
    ,$boolIncludeZipCodes === true?'INNER JOIN restaurants_to_zip_codes rz ON r.restaurants_id = rz.restaurants_id ':''
    ,empty($arrSQLFilters)?'':' WHERE 
'.implode(' AND ',$arrSQLFilters)   
    ,$boolIncludeZipCodes === true?'GROUP BY r.restaurants_id':''
    ,$offset, $rowsperpage
);?>

 

Notice the WHERE Cluase I have put, (r.zip = "$strZipCode") but it will return that there is an sql syntax error. Notice the $arrSQLFilters is equal to

 <?php   $arrSQLFilters[] = sprintf(
            "r.zip LIKE '%s'"
            ,"%$strZipCode%" ?>

 

This bit of code is found on top of the query, I have change the LIKE clause to

<?php
     "r.zip = '%s'"
     ,$strZipCode
?>

 

Without any sucess.

 

Any help would be appriciated.

 

Thank!

It's probably because you are not handling your form data properly. PHP gives you SUPER GLOBALS that hold your incoming data based on how the data is being passed to your script. If you are using POST then the variable "strZipCode" will be available by accessing it's SUPER GLOBAL POST name $_POST['strZipCode'] If the form data is coming by way of GET it will be available by accessing the SUPER GLOBAL GET by name $_GET['strZipCode']. I recommend you read up on SUPER GLOBALS so you understand what you did wrong! The way you are doing it now will only work if register_globals is OFF in you PHP.INI file, and that isn't recommend period. Also always validate incoming data, don't trust any data that is not hard coded in your script!

 

p!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.