Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2011, 12:57 PM   PM User | #1
FlashDance
Regular Coder

 
Join Date: Sep 2011
Posts: 274
Thanks: 38
Thanked 0 Times in 0 Posts
FlashDance can only hope to improve
Am I suppose to brake out of this?

2 x
Quote:
unexpected T_ENCAPSED_AND_WHITESPACE
PHP Code:
$query "select zone from delivery where country = '$_POST['country']'"
Am I suppose to be braking out of '$_POST['country']' or something?
FlashDance is offline   Reply With Quote
Old 10-08-2011, 01:21 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,496
Thanks: 44
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
PHP Code:
$query "select zone from delivery where country = '$_POST[country]'"
You don't use the single quotes in an array inside double quotes.

Your SQL is formed like this "select zone from .." - Note the double quotes.

So.. when an array is used inside double quotes you don't use the single quotes inside it. Because we're already inside double quotes PHP knows that it should inspect variables including arrays and it will replace them in the SQL with their real value.

You could also use {$_POST['country']} in the SQL instead.

For more info about quotes and the T_WHITESPACE error see the link in my signature about quotes.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Users who have thanked tangoforce for this post:
FlashDance (10-08-2011)
Old 10-08-2011, 01:24 PM   PM User | #3
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
PHP Code:
$query "SELECT zone FROM delivery WHERE country='".$_POST['country']."'"
myfayt is offline   Reply With Quote
Users who have thanked myfayt for this post:
FlashDance (10-08-2011)
Old 10-08-2011, 01:59 PM   PM User | #4
FlashDance
Regular Coder

 
Join Date: Sep 2011
Posts: 274
Thanks: 38
Thanked 0 Times in 0 Posts
FlashDance can only hope to improve
tangoforce, I did not know that about the single quotes inside double quotes. You explained it really well. I've had four people try to explain that to me, but it didn't get through. Yours did. Thanks for the info mate!

myfayt, I feel like the biggest idiot! I could have swore I used that combination of quotes and full stops, infact, I am sure it was the first attempt I made to brake out! Anyhow, I must have... I donno, I'm a big goose! Thanks man!
FlashDance is offline   Reply With Quote
Old 10-08-2011, 02:03 PM   PM User | #5
FlashDance
Regular Coder

 
Join Date: Sep 2011
Posts: 274
Thanks: 38
Thanked 0 Times in 0 Posts
FlashDance can only hope to improve
Man, I'm fkn FUMING! 25 minutes lost because of my stupidity!
FlashDance is offline   Reply With Quote
Old 10-08-2011, 03:27 PM   PM User | #6
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,496
Thanks: 44
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by FlashDance View Post
myfayt, I feel like the biggest idiot! I could have swore I used that combination of quotes and full stops, infact, I am sure it was the first attempt I made to brake out! Anyhow, I must have... I donno, I'm a big goose! Thanks man!
Yes you did try that combination.. but you forgot to wrap it in {} braces which tell php to treat it normally - as if it were not inside double quotes.

Don't get annoyed with yourself, its part of learning. When I was first learning I spend HOURS and sometimes DAYS trying to work out why my code was showing errors.. only to find i'd missed a semicolon. It's just one of those things you learn from.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 10-08-2011, 06:12 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Using {$_POST['country']} would be the more accurate way to represent this within a string. This is referred to as complex evaluation.
This would be done for consistency only.
Constants in PHP are not parsed within the context of a string. I personally recommend breaking out of strings completely (or using a formatter which I usually do) instead of doing either of the above. Functions and methods also require complex evaulation.
Here's an example as to how easy it would be to err:
PHP Code:
<?php
define
('T''C');
$a = array('T' => 'Test''C' => 'Cat');

print 
"This is a string with $a[T] in it.<br />" PHP_EOL;
print 
"This is a string with {$a['T']} in it.<br />" PHP_EOL;
print 
"This is a string with {$a[T]} in it.<br />" PHP_EOL;

class 
Obj
{
    private 
$v;
    public function 
__construct($in)
    {
        
$this->$in;
    }

    public function 
p()
    {
        return 
$this->v;
    }
}

$o = new Obj('test');

print 
"This is a string with {$o->p()} in it.<br />" PHP_EOL;
print 
"This is a string with $o->p() in it.<br />" PHP_EOL;

?>
Results in:
Code:
This is a string with Test in it.<br />
This is a string with Test in it.<br />
This is a string with Cat in it.<br />
This is a string with test in it.<br />
This is a string with () in it.<br />
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:23 PM.


Advertisement
Log in to turn off these ads.