RE: [PHP] i'm beginner and i couldn't solve the problem
| From: | Ford, Mike | Date: | Wed, 08 Dec 2004 17:22:48 +0000 |
| Subject: | RE: [PHP] i'm beginner and i couldn't solve the problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-203739@lists.php.net to get a copy of this message | ||
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
On 08 December 2004 16:58, Chris W. Parker wrote:
> Ford, Mike <mailto:M.Ford@leedsmet.ac.uk>
> on Wednesday, December 08, 2004 5:18 AM said:
>
> > > $sql = "INSERT INTO testTable values ('',
> > > '$_POST[testField]')";
> > >
> > > should be
> > >
> > > $var = $_POST["testField"];
> > > $sql = "INSERT INTO testTable values ('', '$var')";
> >
> > Nope -- the original is functionally identical to your suggested
> > correction.
>
> Hmm... I thought arrays don't work correctly within a string
> unless they
> are wrapped with { and }?
They do if you treat them right.
> <?php
>
> $sql = "INSERT INTO ... ('',
> '{$_POST['testField']}')";
>
> Also you should always wrap the index name in single quotes (or maybe
> double quotes work also.)
RTFM -- this example taken from
http://uk2.php.net/manual/en/language.types.string.php#language.types.string
.parsing.simple illustrates the topic pretty well:
$fruits = array('strawberry' => 'red', 'banana' =>
'yellow');
// Works but note that this works differently outside string-quotes
echo "A banana is $fruits[banana].";
// Works
echo "A banana is {$fruits['banana']}.";
// Works but PHP looks for a constant named banana first
// as described below.
echo "A banana is {$fruits[banana]}.";
// Won't work, use braces. This results in a parse error.
echo "A banana is $fruits['banana'].";
// Works
echo "A banana is " . $fruits['banana'] . ".";
// Works
echo "This square is $square->width meters broad.";
// Won't work. For a solution, see the complex syntax.
echo "This square is $square->width00 centimeters broad.";
?>
For anything more complex, you should use the complex syntax.
I wouldn't personally encourage the "... $fruits[banana] ..." style, but
it's valid and works perfectly well.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211