从数据库值创建阵列时出错的疑难解答


Troubleshooting error when creating array from database values

有人能看到我用这个php数组做错了什么吗?我正试图创建一个动态数组,但不知怎么的,它不起作用,并且在试图从浏览器访问它时出现了服务器内部错误

$index = 0;
$columnIndex = 0
while($row = mysqli_fetch_array($result, MYSQL_NUM))
{
  $test = array();
  $test[$arrayIndex] = $row[$columnIndex];
  $coumnIndex = 1;
  if(is_string($row[$number]))
  { 
    preg_match("/(?:'d+'.)?(?:'s*)?$stop?(?:'s*)?(.*):(.*)",{$row[$columnIndex]},$match1);     
    $test[$index] = '<p> <strong> . $match1[1] . </strong> . $match1[2] . </p>';
  }      
  ++$arrayIndex; 
  ++$columnIndex;   
}
$jsonData = json_encode($test);
echo $jsonData;

您得到错误的可能性很大,因为您正在执行$test[$arrayIndex] = $row[$columnIndex];,而我没有看到定义的$arrayIndex(因此它为空)。不能设置null数组索引。

如果你想要一个简单的PHP动态数组,你可以这样做:

$test = array();
while(/* however you want to define your loop */) {
    $test[] = /* something */;
}

您将得到一个数组$test,它包含您在每行/* something */中放入的任何内容。查看PHP数组页面上题为"使用方括号语法创建/修改"的部分。

您有一个打字错误:

 $coumnIndex = 1; 

应为columnIndex

在尝试连接$text[$index]行上的变量之前,您也忽略了关闭引号。