I learned recently that you can put an array into a string using the braces
operator like so:

$string = "Here's an interpolated {$array['key']}";

This is really handy for sql statements and other neat things.

You can use indexed arrays too. Actually, all arrays in php are hashes
(associative). That's why they're so quick.

Happy interpolation!

Matt Friedman



----- Original Message -----
From: "Frank M. Kromann" <[EMAIL PROTECTED]>
To: "Michael Champagne" <[EMAIL PROTECTED]>
Cc: "PHP General Mailing List" <[EMAIL PROTECTED]>
Sent: Friday, March 23, 2001 8:25 PM
Subject: Re: [PHP] PHP error parsing SQL string containing assoc. array


> Hi All,
>
> PHP is unable to resolve the variable you are trying to use within a
stting. If you use complex array structures you have to write it like this:
>
>
> $sql = "insert into sessions(session_id, user_id, session_expiration)
values ('$sess_id', " . $user_fields['USER_ID'] . "', sysdate + (" .
$user_fields'MINS_TILL_EXPIRE'] . " * 1/1440))";
>
> or use
>
> $sql = sprintf("insert into sessions(session_id, user_id,
session_expiration) values ('%s', sysdate + (%i * 1/1440))", $sess_id',
$user_fields['USER_ID'], $user_fields['MINS_TILL_EXPIRE']);
>
>
> Your sample code also mised a ' after the first variable !
>
> - Frank
>
> > I'm trying to write a sql statement (for Oracle) that uses an
associative
> > array element as a dynamic value.  PHP is choking on this line:
> >
> > $sql = "insert into sessions(session_id, user_id, session_expiration)
values ('$sess_id', $user_fields['USER_ID'], sysdate +
($user_fields['MINS_TILL_EXPIRE'] * 1/1440))"
> >
> > I get this error back from PHP:
> >
> > Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or
`T_NUM_STRING'
> >
> > Can I not use my associative array like this?
> >
> > --
> > Michael Champagne, Software Engineer
> > Capital Institutional Services, Inc.
> > wk: [EMAIL PROTECTED]
> > hm: [EMAIL PROTECTED]
> >
> >
> >
> > ******************************************************************
> > This communication is for informational purposes only.  It is not
> > intended as an offer or solicitation for the purchase or sale of
> > any financial instrument or as an official confirmation of any
> > transaction, unless specifically agreed otherwise.  All market
> > prices, data and other information are not warranted as to
> > completeness or accuracy and are subject to change without
> > notice.  Any comments or statements made herein do not
> > necessarily reflect the views or opinions of Capital Institutional
> > Services, Inc.  Capital Institutional Services, Inc. accepts no
> > liability for any errors or omissions arising as a result of
> > transmission.  Use of this communication by other than intended
> > recipients is prohibited.
> > ******************************************************************
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > To contact the list administrators, e-mail: [EMAIL PROTECTED]
> >
> >
> >
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>
>
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to