$_GET syntax use [Solved]
Moderator: General Moderators
$_GET syntax use [Solved]
Why is this not working ???
$query_test = 'SELECT * FROM table WHERE var LIKE $_GET['varName']';
I check varName and it has the good value.
Thanx in advance for help.
$query_test = 'SELECT * FROM table WHERE var LIKE $_GET['varName']';
I check varName and it has the good value.
Thanx in advance for help.
Last edited by desb01 on Mon Jun 04, 2007 9:15 pm, edited 1 time in total.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Re: $_GET syntax use
desb01 wrote:Why is this not working ???
$query_test = 'SELECT * FROM table WHERE var LIKE $_GET['varName']';
I check varName and it has the good value.
Thanx in advance for help.
Code: Select all
$query_test = "SELECT * FROM table WHERE var LIKE $_GET[varName]";Also, use brackets when working with arrays when inside doublequotes, or do it outside of the doublequotes;
Code: Select all
$query_test = "SELECT * FROM table WHERE var LIKE {$_GET['varName']}";Code: Select all
$query_test = "SELECT * FROM table WHERE var LIKE ".$_GET['varName'];You also need to take care of sql injections, see http://de2.php.net/security.database.sql-injection
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
:(
feyd | Please use
The followings don't
The following works
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
First of all, thank you for taking the time answering my question.
Next, I tried all of the above without any success.
The following workCode: Select all
$query_cities = 'SELECT * FROM cities WHERE country LIKE "USA"';Code: Select all
$query_cities = "SELECT * FROM cities WHERE country LIKE $_GET[countryName]";
$query_cities = "SELECT * FROM cities WHERE country LIKE ".$_GET['countryName'];
$query_cities = "SELECT * FROM cities WHERE country LIKE {$_GET['countryName']}";
$query_cities = "SELECT * FROM cities WHERE country LIKE $_GET[countryName]";Code: Select all
switch($_GET['countryName']){
case "USA":
echo "obj.options[obj.options.length] = new Option('New York','1');\n";
echo "obj.options[obj.options.length] = new Option('Los Angeles','2');\n";
echo "obj.options[obj.options.length] = new Option('Pittsburg','3');\n";
echo "obj.options[obj.options.length] = new Option('New Hamshire','4');\n";
break;
}feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Almost every combination. You forgot to put quotes around the country name. And at least use the database specific escaping function if you are not going to do anything else:
Code: Select all
$country = databasespecific_escape($_GET['countryName']);
$query_cities = "SELECT * FROM cities WHERE country LIKE '$country'";(#10850)
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
All of your queries are incorrect syntax. Try these:
Code: Select all
$query_cities = "SELECT * FROM cities WHERE country LIKE '$_GET[countryName]'";
$query_cities = "SELECT * FROM cities WHERE country LIKE '".$_GET['countryName']."'";
$query_cities = "SELECT * FROM cities WHERE country LIKE '{$_GET['countryName']}'";
$query_cities = "SELECT * FROM cities WHERE country LIKE '$_GET[countryName]'";- neel_basu
- Forum Contributor
- Posts: 454
- Joined: Wed Dec 06, 2006 9:33 am
- Location: Picnic Garden, Kolkata, India
THis will work
EDIT
-------
Oh! Sorry I overlookedd the previous post. Everah has answered you.
Code: Select all
$query_cities = "SELECT * FROM `cities` WHERE `country` LIKE '".$_GET['countryName']."'";-------
Oh! Sorry I overlookedd the previous post. Everah has answered you.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA