|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-02-22 06:45 UTC] jpaulomf at terra dot com dot br
Here is the code:
error_reporting(E_ALL);
$table = array('admin' => 'md_adm'
,'users' => 'usuarios'
,'tables' => 'md_tables'
,'product' => 'md_prods'
);
if(!connect_mydb()) die("unable to connect or select db");
if(isset($table['users'])) { // => works OK
echo "$table[users] will warn me!<br>"; // => warning
echo "$table['users']"; // => no warning
}
$query = "SELECT * from $table['users']";
$result = mysql_query($query); // => PARSE ERROR!!
//But if I delete the '' in $query it works ok:
$query = "SELECT * from $table[users]";
$result = mysql_query($query); // => Query done!!
According to PHP Manual in Arrays : "Why is $foo[bar] wrong?" the syntax $foo[bar] is deprecated despite working.
Jo?o Paulo M. Fischer
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 17 17:00:01 2025 UTC |
Sounds very strange. Can you do a var_dump() on the sql string? Can you also test and report back if it works with "SELECT * from {$table['users']}" ?It seems we cannot use the correct syntax $table['users'] inside the two ""... This will work: $result = mysql_query("SELECT * from ".$table['users']); This wont work: $result = mysql_query("SELECT * from $table['users']"); This will, but should send a warning and it isnt: $result = mysql_query("SELECT * from $table[users]"); The errors happens everywere I call any array with the '' or "" like $table["something"] or $table['something'] inside ""s...Use the {$array['key']} syntax as I told you. However, I can't remember if "foo $array['bar']" ever worked or not right now. Someone else?Hey man WOW, Im impressed, you have already replied!!! Yes, this solution works: $query = "SELECT * from {$table['users']}"; But shouldn't "Blah $table['users']" work as well? Shouldn't PHP at least send a warning when using "$table[users]" even if the other "$arr['foo']" syntaxes where wrong? Thanks!Ok, but I think it should be seen as a BUG, since we cannot call the array as it should be and we are able to call it as it shouldn't. PHP is expected work with codes like: sql_query("SELECT * from $array['table']"); and its expected to (but doesnt have to) work with: sql_query("SELECT * from $array[table]"); and in such case its expected to give a warning... in both cases the behavior of 4.1.1 is buggy... Thanks for your time people! Best Regards Jo?o Paulo M. Fischer> PHP is expected work with codes like: > sql_query("SELECT * from $array['table']"); one *could* expect that it understood this, that's what my feature request is about, but "PHP is expected" is not right > and its expected to (but doesnt have to) work with: > sql_query("SELECT * from $array[table]"); > and in such case its expected to give a warning... no, this is the current and expected behavior no warning is generated here as no define() substitution is done in strings > in both cases the behavior of 4.1.1 is buggy... it has been like this ever since 3.0, so you can't call this 'buggy', call it 'unexpected behaviour', 'inconsistent design' or whatever, but it is not a bug ...> no warning is generated here as no define() substitution > is done in strings Why no define() is applied to the string?? From the PHP manual: [quote]Why is $foo[bar] wrong?[/quote] [quote]...there must be an expression between the square brackets ('[' and ']').[/quote] Since there is no expression, the workaround IS to make a define() to the string or integer between the []s and set the value to its own name. error_reporting(E_ALL); define("ke","key"); $foo['key'] = 'aaa'; $foo['ke'] = 'bbb'; echo "<br>It is<br>" ."Foo[ke] ==> $foo[ke]"; echo "<br><br>It should be:<br>" ."Foo[ke] ==> ".$foo[ke]; Ok, we can deal with this, but thats not the way it should work according with the good sense and the manual.. RegardsConstants are not looked for within strings, this is why "$foo[key]" does not generate a warning here. But outside of strings this is a different story. define ('a','b'); $arr = array('a' => 'apple', 'b' => 'banana'); print $arr['a']; // apple print $arr[a]; // banana print "$arr[a]" // apple print "$arr['a']" // parse error print "{$arr['a']}" // apple This bug report is a feature request for "$arr['a']" to print apple. Regarding print $arr[a] above, if 'a' was not a defined constant, a warning would exist but apple is still printed (if key 'a' exists).