"未定义的偏移"无法找到PHP的原因

时间:2014-04-09 08:41:04

标签: php html sql arrays

我找不到Undefined offset: 0 in错误的原因。我有数组,我给了范围索引:/ 我是PHP + SQL的新手,但我必须为学校任务做这件事。

如果我理解正确,我的get_searched_grad函数会返回数组数组......

错误位于我访问$grd数组$grd[someNumber]

的行中
<?php
  foreach (get_searched_grad($search_key) as $grd) {
?>
  <tr>
    <th>
      <?php
        $grd[0];
      ?>                         
    </th>
    <th>
      <?php
        $grd[1];
      ?>                         
    </th>
    <th>
      <?php
        $grd[2];
      ?>                         
    </th>
    <th>
      <?php
        $grd[3];
      ?>                         
    </th>
  </tr>

<?php
  }
?>

在get_searched_grad()函数中我有:

function get_searched_grad($searchWord) {

  $gradovi = array();

  if ($searchWord != 0) {
    $query = mysql_query("SELECT `NAZIV_GRADA`, `PTT`, `BROJ_STANOVNIKA`, `NAZIV_DRZAVE` FROM `grad`,`drzava` WHERE `NAZIV_GRADA` LIKE '%{$searchWord}%' AND drzava.ID_DRZAVE = grad.ID_DRZAVE");
  } else {
    $query = mysql_query("SELECT `NAZIV_GRADA`, `PTT`, `BROJ_STANOVNIKA`, `NAZIV_DRZAVE` FROM `grad`,`drzava` WHERE drzava.ID_DRZAVE = grad.ID_DRZAVE");
  }
  while ($row = mysql_fetch_assoc($query)) {
    $gradovi [] = $row;
  }
  return $gradovi;
}

一些变量和表格行名称都在塞尔维亚语上:)很抱歉......

array(2){
    [
        0
    ]=>array(4){
        [
            "NAZIV_GRADA"
        ]=>string(7)"Beograd"[
            "PTT"
        ]=>string(5)"11000"[
            "BROJ_STANOVNIKA"
        ]=>string(6)"400000"[
            "NAZIV_DRZAVE"
        ]=>string(6)"Srbija"
    }[
        1
    ]=>array(4){
        [
            "NAZIV_GRADA"
        ]=>string(9)"Zrenjanin"[
            "PTT"
        ]=>string(5)"23000"[
            "BROJ_STANOVNIKA"
        ]=>string(5)"12000"[
            "NAZIV_DRZAVE"
        ]=>string(6)"Srbija"
    }
}

3 个答案:

答案 0 :(得分:1)

这很可能意味着您的数据库没有返回任何结果。要检查它,您可以使用isset方法,该方法仅在范围中存在变量且不为空时才返回true。它也适用于数组下标。

所以如果我们有一个数组:

$data = array(
  'stack' => 'overflow'
);

我们并不总是确定数组中存在一个键,所以要找出你在数组中使用下标调用isset。

if(isset($data['stack'])) {
  echo $data['stack'];
} else {
  echo 'stack was not a key inside the data array';
}

您的方法可以使用一些重新分解。我假设您正在尝试通过执行!= 0语句来检查是否已将空字符串或空值传递给它。检查字符串是否为空或null的更好方法是:

if( isset($searchword) && strlen($searchword) > 0) {
  // at this point we have at least one character to search for.
}

使用数组数组,因此需要遍历它们。最后,回答你的问题。您需要将实现更改为:

$data = get_searched_grad($search_key);
for($i = 0; $i < count($data) $i++) {
  $row = $data[$i];
  echo $row['NAZIV_GRADA'] . "<br />";
  echo $row['PTT'] . "<br />";
  //...
}

答案 1 :(得分:0)

您的var转储显示

array(2) {
  [0]=> array(4) { 
             ["NAZIV_GRADA"]=> string(7) "Beograd" 
             ["PTT"]=> string(5) "11000" 
             ["BROJ_STANOVNIKA"]=> string(6) "400000" 
             ["NAZIV_DRZAVE"]=> string(6) "Srbija" 
   } 
   [1]=> array(4) { 
             ["NAZIV_GRADA"]=> string(9) "Zrenjanin" 
             ["PTT"]=> string(5) "23000" 
             ["BROJ_STANOVNIKA"]=> string(5) "12000" 
             ["NAZIV_DRZAVE"]=> string(6) "Srbija" 
   } 
}

所以你应该做点什么

$data = get_searched_grad($search_key) ;
foreach ($data as $grd) {
                    ?>
                    <tr>
                        <th> <?php
                            $grd["NAZIV_GRADA"];
                            ?>                         
                        </th>
                        <th> <?php
                            $grd["PTT"];
                            ?>                         
                        </th>
                        <th> <?php
                            $grd["BROJ_STANOVNIKA"];
                            ?>                         
                        </th>
                        <th> <?php
                            $grd["NAZIV_DRZAVE"];
                            ?>                         
                        </th>
                    </tr>

                    <?php
                }

为什么它对你不起作用。既然你正在寻找

your_array[0][0]
your_array[0][1]
        etc and this will fail since its an associative array.

答案 2 :(得分:0)

foreach(get_searched_grad($search_key) as $grd) {
    if(is_array($grd)) {
        foreach($grd as $key => $value) {
            echo $grd[$key];
        }
    }
}