ID:               15673
 Updated by:       [EMAIL PROTECTED]
-Summary:          Quotes crash MySQL queries: $array['val'] = Parse
                   Error, but  $array[val] works
 Reported By:      [EMAIL PROTECTED]
 Status:           Open
 Bug Type:         Arrays related
 Operating System: Linux 2.4.5
 PHP Version:      4.1.1
 New Comment:

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...


Previous Comments:
------------------------------------------------------------------------

[2002-02-22 07:06:12] [EMAIL PROTECTED]

Sorry: The error occurs when we define the $query:

$query = "SELECT * from $table[users]"; // => This causes the parse
error

echo "Table $table['users']" //=> Parse Error too
echo "Table".$table['users']; //=>OK
$query = "SELECT * from ".$table['users']; //=> OK TOO

------------------------------------------------------------------------

[2002-02-22 06:59:46] [EMAIL PROTECTED]

d

------------------------------------------------------------------------

[2002-02-22 06:55:12] [EMAIL PROTECTED]

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']}"

?

------------------------------------------------------------------------

[2002-02-22 06:45:23] [EMAIL PROTECTED]

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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=15673&edit=1

Reply via email to