Hmm..why doesnt this work? [message #176074] |
Tue, 22 November 2011 16:10 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
$old=(mysql_fetch_assoc(mysql_query(sprintf(
"select login_name from employees where id='%d'",
$_POST['id']))))['id'];
Bitching about unexpected '['
But mysql_fetch_assoc() returns an array so what's wrong with
mysql_fetch_assoc()['id'];
??
|
|
|
Re: Hmm..why doesnt this work? [message #176075 is a reply to message #176074] |
Tue, 22 November 2011 16:19 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
The Natural Philosopher wrote:
>
>
> $old=(mysql_fetch_assoc(mysql_query(sprintf(
> "select login_name from employees where id='%d'",
> $_POST['id']))))['id'];
>
>
> Bitching about unexpected '['
>
> But mysql_fetch_assoc() returns an array so what's wrong with
>
> mysql_fetch_assoc()['id'];
>
> ??
cos this DOES work...
$oldarr=(mysql_fetch_assoc(mysql_query(sprintf("select login_name from
employees where id='%d'",$_POST['id']))));
$old=$oldarr['id'];
This is like using crap C compilers back in the 80's...
|
|
|
Re: Hmm..why doesnt this work? [message #176076 is a reply to message #176074] |
Tue, 22 November 2011 16:22 |
tony
Messages: 19 Registered: December 2010
Karma: 0
|
Junior Member |
|
|
In article <jaghhf$867$1(at)news(dot)albasani(dot)net>,
The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:
>
>
> $old=(mysql_fetch_assoc(mysql_query(sprintf(
> "select login_name from employees where id='%d'",
> $_POST['id']))))['id'];
>
>
> Bitching about unexpected '['
>
> But mysql_fetch_assoc() returns an array so what's wrong with
>
> mysql_fetch_assoc()['id'];
Or even (mysql_fetch_assoc())['id'] :-)
Firstly, id isn't in the field list. Only login_name is. But that's
semantic, not syntax.
But you shouldn't dereference the return value of mysql_fetch_assoc()
without first checking it for success:
$res = mysql_fetch_assoc(mysql_query(sprintf(
"select login_name from employees where id=%d",
$_POST['id'])));
if ($res !== false) {
$old = $res['login_name'];
} else {
// handle the error, including checking mysql_error()
}
Cheers
Tony
--
Tony Mountifield
Work: tony(at)softins(dot)co(dot)uk - http://www.softins.co.uk
Play: tony(at)mountifield(dot)org - http://tony.mountifield.org
|
|
|
Re: Hmm..why doesnt this work? [message #176078 is a reply to message #176076] |
Tue, 22 November 2011 17:24 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
Tony Mountifield wrote:
> In article <jaghhf$867$1(at)news(dot)albasani(dot)net>,
> The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:
>>
>> $old=(mysql_fetch_assoc(mysql_query(sprintf(
>> "select login_name from employees where id='%d'",
>> $_POST['id']))))['id'];
>>
>>
>> Bitching about unexpected '['
>>
>> But mysql_fetch_assoc() returns an array so what's wrong with
>>
>> mysql_fetch_assoc()['id'];
>
> Or even (mysql_fetch_assoc())['id'] :-)
>
yes, well exactly
> Firstly, id isn't in the field list. Only login_name is. But that's
> semantic, not syntax.
>
Indeed, and that fixes the bug I was looking at now..thanks :-)
> But you shouldn't dereference the return value of mysql_fetch_assoc()
> without first checking it for success:
>
Yes, but the point is that syntactically I can't it seems.
And in this case, its guaranteed to succeed, because ...select....where
id='$id' has already succeeded...because that's how $_POST['id'] gets
set!!!.. and there's no WAY to delete a record in the programs that
access this database!
I suppose some one with admin privileges MIGHT hack it..but then FFS the
damage that could be dione is not something a simple error check could
repair anyway
The point remains. Why does mysql_fetch_assoc(.....)['name'] return a
SYNTAX error?
|
|
|
|
Re: Hmm..why doesnt this work? [message #176081 is a reply to message #176080] |
Tue, 22 November 2011 17:49 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
Tony Mountifield wrote:
> In article <jaglt2$k0v$1(at)news(dot)albasani(dot)net>,
> The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:
>> The point remains. Why does mysql_fetch_assoc(.....)['name'] return a
>> SYNTAX error?
>
> I expect it's just because the creators of PHP just didn't think of it.
> I can't find any comment on the topic in the only PHP manual.
>
> Unlike C and Perl, which were actually DESIGNED and have a regular,
> orthogonal and consistent syntax, PHP appears to have just grown like
> Topsy, without any consideration of consistency or regularity (at least
> in the early days, but then you have an installed base to remain
> compatible with, so it's hard to fix the inconsistencies).
>
Now THAT is a perfectly reasonable explanation..
But implementing that would not actually upset anything that exists:
merely make something that didn't work before (and arguably should)
work..now!
(or in some future release).
> Cheers
> Tony
|
|
|
Re: Hmm..why doesnt this work? [message #176082 is a reply to message #176081] |
Tue, 22 November 2011 17:57 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
The Natural Philosopher wrote:
> Tony Mountifield wrote:
>> In article <jaglt2$k0v$1(at)news(dot)albasani(dot)net>,
>> The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:
>>> The point remains. Why does mysql_fetch_assoc(.....)['name'] return a
>>> SYNTAX error?
>>
>> I expect it's just because the creators of PHP just didn't think of it.
>> I can't find any comment on the topic in the only PHP manual.
>>
>> Unlike C and Perl, which were actually DESIGNED and have a regular,
>> orthogonal and consistent syntax, PHP appears to have just grown like
>> Topsy, without any consideration of consistency or regularity (at least
>> in the early days, but then you have an installed base to remain
>> compatible with, so it's hard to fix the inconsistencies).
>>
>
> Now THAT is a perfectly reasonable explanation..
>
>
> But implementing that would not actually upset anything that exists:
> merely make something that didn't work before (and arguably should)
> work..now!
>
> (or in some future release).
>
>
>
>> Cheers
>> Tony
Ah..it looks like I am not the only one to bitch, and, indeed, it HAS
been fixed in later releases...
https://wiki.php.net/rfc/functionarraydereferencing
looks like 5.4 or later has this..
sadly even latest Debian stable is only 5.3
|
|
|