php mysql用数组更新值[关闭]
问题描述:
$array['a']['b']=50;
mysql_query("update table set name=name-$array['a']['b']");
It show's error. Why it is not working? How fix it?
Thanks
$ array ['a'] ['b'] = 50;
mysql_query(“update table” set name = name- $ array ['a'] ['b']“);
code> pre>
显示错误。 为什么不工作? 如何解决? p>
谢谢 p>
div>
答
PHP's parser is not "greedy".
echo "$arr[a][b]"
is actually parsed as if it was:
echo $arr['a'];
echo '[b]';
and would produce as output
Array[b]
You need to use the {}
notation for this to work as you've written it:
mysql_query("update table set name=name-{$array['a']['b']}");
^-- ^--
答
This should have more chances to work:
$array['a']['b'] = 50;
// concatenate instead of accessing the array element directly into the string
// + intval() to ensure that the query will be correct
// (supposing that name is an number field)
mysql_query("update table set name = name - ".intval($array['a']['b']));
答
Use the .
operator to concatenate strings:
mysql_query("update table set name=name-" . $array['a']['b']);
答
try this:
mysql_query("update table set name='name-".$array['a']['b']."'");