Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3868 > unrolled thread

Hmm..why doesnt this work?

Started byThe Natural Philosopher <tnp@invalid.invalid>
First post2011-11-22 16:10 +0000
Last post2011-11-22 17:57 +0000
Articles 7 — 2 participants

Back to article view | Back to comp.lang.php


Contents

  Hmm..why doesnt this work? The Natural Philosopher <tnp@invalid.invalid> - 2011-11-22 16:10 +0000
    Re: Hmm..why doesnt this work? The Natural Philosopher <tnp@invalid.invalid> - 2011-11-22 16:19 +0000
    Re: Hmm..why doesnt this work? tony@mountifield.org (Tony Mountifield) - 2011-11-22 16:22 +0000
      Re: Hmm..why doesnt this work? The Natural Philosopher <tnp@invalid.invalid> - 2011-11-22 17:24 +0000
        Re: Hmm..why doesnt this work? tony@mountifield.org (Tony Mountifield) - 2011-11-22 17:44 +0000
          Re: Hmm..why doesnt this work? The Natural Philosopher <tnp@invalid.invalid> - 2011-11-22 17:49 +0000
            Re: Hmm..why doesnt this work? The Natural Philosopher <tnp@invalid.invalid> - 2011-11-22 17:57 +0000

#3868 — Hmm..why doesnt this work?

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2011-11-22 16:10 +0000
SubjectHmm..why doesnt this work?
Message-ID<jaghhf$867$1@news.albasani.net>

$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'];

??

[toc] | [next] | [standalone]


#3869

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2011-11-22 16:19 +0000
Message-ID<jagi2l$90h$1@news.albasani.net>
In reply to#3868
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...

[toc] | [prev] | [next] | [standalone]


#3870

Fromtony@mountifield.org (Tony Mountifield)
Date2011-11-22 16:22 +0000
Message-ID<jagi7c$ath$1@softins.clara.co.uk>
In reply to#3868
In article <jaghhf$867$1@news.albasani.net>,
The Natural Philosopher  <tnp@invalid.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@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org

[toc] | [prev] | [next] | [standalone]


#3872

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2011-11-22 17:24 +0000
Message-ID<jaglt2$k0v$1@news.albasani.net>
In reply to#3870
Tony Mountifield wrote:
> In article <jaghhf$867$1@news.albasani.net>,
> The Natural Philosopher  <tnp@invalid.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?

[toc] | [prev] | [next] | [standalone]


#3874

Fromtony@mountifield.org (Tony Mountifield)
Date2011-11-22 17:44 +0000
Message-ID<jagn15$cl2$1@softins.clara.co.uk>
In reply to#3872
In article <jaglt2$k0v$1@news.albasani.net>,
The Natural Philosopher  <tnp@invalid.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@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org

[toc] | [prev] | [next] | [standalone]


#3875

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2011-11-22 17:49 +0000
Message-ID<jagnbq$n16$1@news.albasani.net>
In reply to#3874
Tony Mountifield wrote:
> In article <jaglt2$k0v$1@news.albasani.net>,
> The Natural Philosopher  <tnp@invalid.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

[toc] | [prev] | [next] | [standalone]


#3876

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2011-11-22 17:57 +0000
Message-ID<jagnqh$ogu$1@news.albasani.net>
In reply to#3875
The Natural Philosopher wrote:
> Tony Mountifield wrote:
>> In article <jaglt2$k0v$1@news.albasani.net>,
>> The Natural Philosopher  <tnp@invalid.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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web