我从数据库中获取信息,将其保存在数组中并以循环结构的形式回显它,当我试图将修改后的信息保存到数据库时,我遇到了问题。
我得到这个错误:
代码:致命错误:[]操作符不支持....
中的字符串
$namesql1 = "SELECT name,date,text,date2 FROM table WHERE something= '$something'";
$nameresult1 = mysql_query($namesql1);
$countrows = mysql_num_rows($nameresult1);
while ($row = mysql_fetch_array($nameresult1, MYSQL_ASSOC)) {
$name[] = $row['name'];
$date[] = $row['date'];
$text[] = $row['text'];
$date2[] = $row['date2 '];
}
/** SOME CODE HERE **/
$wrotesql = "UPDATE service_report SET name ='$name' , $date = '$date',$text = '$text[$nro]', ser_date = '$date2[$nro]' WHERE something = '$something')";
$wroteresult = mysql_query($wrotesql);
谁能给我提示一下我做错了什么吗? 当尝试在字符串上使用短数组push语法时,会得到此错误。
例如:
$foo = 'foo';
$foo[] = 'bar'; // ERROR!
我猜你的$name
, $date
, $text
或$date2
变量中的一个或多个已被初始化为字符串。
编辑:再看看你的问题,看起来你实际上不想使用它们作为数组,因为你把它们作为字符串进一步处理。
如果是,将赋值更改为
$name = $row['name'];
$date = $row['date'];
$text = $row['text'];
$date2 = $row['date2'];
似乎有一些问题与PHP 7和代码使用empty-index数组推语法。
说明一下,这些在PHP 7+中工作得很好。$previouslyUndeclaredVariableName[] = 'value'; // creates an array and adds one entry
$emptyArray = []; // creates an array
$emptyArray[] = 'value'; // pushes in an entry
不工作的原因试图使用empty-index压入任何声明为字符串,数字,对象等的变量,即
$declaredAsString = '';
$declaredAsString[] = 'value';
$declaredAsNumber = 1;
$declaredAsNumber[] = 'value';
$declaredAsObject = new stdclass();
$declaredAsObject[] = 'value';
您可能已经将$name
, $date
, $text
或$date2
定义为字符串,如:
$name = 'String';
如果你把它当作一个数组它会给出致命错误:
$name[] = 'new value'; // fatal error
要解决这个问题,只需在循环开始处添加以下代码:
$name = array();
$date = array();
$text = array();
$date2 = array();
这将重置它们的值为array,然后你就可以使用它们作为数组。
从PHP 7.0迁移中描述了这种行为。/
字符串不再支持空索引操作符将空索引操作符应用于字符串(例如$str[] = $x)会抛出致命错误,而不是无声地转换为数组。
在我的例子中,它只是一个初始化。我通过用$foo=[]
替换$foo=''
来修复它。
$foo='';
$foo[]='test';
print_r($foo);
这在PHP 5.6中是可用的在PHP 7+中,你应该首先声明数组
$users = array(); // not $users = ";
$users[] = "762";
在将变量声明为字符串然后将其写入数组时,我得到了相同的错误。这就是它的工作方式,没有错误
$name = array();
$name[] = $row['name'];
解决!
$a['index'] = [];
$a['index'][] = 'another value';
$a['index'][] = 'another value';
$a['index'][] = 'another value';
$a['index'][] = 'another value';
我同意杰里米·杨对菲尔斯的回答的评论:
我发现这可能是与从PHP 5到PHP 7。PHP 5更能容忍歧义变量是数组还是不是PHP 7的数组。在大多数情况下,解决方案是显式声明数组,如本答案所述。
我只是在php5迁移到php7后解决Wordpress插件的问题。由于插件代码依赖于用户输入,并且它本质上是作为字符串或数组在代码中使用的,因此我添加了以下代码以防止致命错误:
if(is_array($variable_either_string_or_array)){
// if it's an array, declaration is allowed:
$variable_either_string_or_array[]=$additionalInfoData[$i];
}else{
// if it's not an array, declaration it as follows:
$variable_either_string_or_array=$additionalInfoData[$i];
}
这是我需要添加的唯一修改,使插件php7-proof。显然不是"最佳实践",我宁愿阅读并理解完整的代码。但是需要一个快速的解决方案。
我也有类似的情况:
$foo = array();
$foo[] = 'test'; // error
$foo[] = "test"; // working fine
这样编码将修复错误
$name=array();
$date=array();
$text=array();
$date2=array();
while ($row = mysql_fetch_array($nameresult1, MYSQL_ASSOC)) {
$name[] = $row['name'];
$date[] = $row['date'];
$text[] = $row['text'];
$date2[] = $row['date2 '];
}
我的另一个错误是:
$arr = [];
$arr[] = 'add';
$arr = array_shift($arr); // <-- return array_shift is boolean not array ;)
//array_shift(array &$array): mixed
$arr[] = 'next'; // become error