FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Hmm..why doesnt this work?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Hmm..why doesnt this work? [message #176074] Tue, 22 November 2011 16:10 Go to next message
The Natural Philosoph is currently offline  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 Go to previous messageGo to next message
The Natural Philosoph is currently offline  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 Go to previous messageGo to next message
tony is currently offline  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 Go to previous messageGo to next message
The Natural Philosoph is currently offline  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 #176080 is a reply to message #176078] Tue, 22 November 2011 17:44 Go to previous messageGo to next message
tony is currently offline  tony
Messages: 19
Registered: December 2010
Karma: 0
Junior Member
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).

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 #176081 is a reply to message #176080] Tue, 22 November 2011 17:49 Go to previous messageGo to next message
The Natural Philosoph is currently offline  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 Go to previous message
The Natural Philosoph is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How to get client mac address in PHP ..?
Next Topic: Thumbnails with PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Nov 25 03:41:18 GMT 2024

Total time taken to generate the page: 0.03062 seconds