duangan4406 2014-04-09 18:17
浏览 146
已采纳

PHP注意:未定义的偏移量:1

i fetch MySql result using PHP array using this Code:

$sql = 'SELECT * FROM ' . LOCATION . ' ORDER BY category ';
$r = $db->query ( $sql ) or error ('Critical Error', mysql_error () );

while ($ROW = $db->fetcharray($r))
{
     if ($ROW[1] == '') //line 15
          $ROW[1] = $ROW['subcategory'];
} 

fetcharray function:

function fetcharray ($query_id)

    {
        if(!$query_id)
        {
            $query_id = $this->query_res;
        }

        if($query_id)
    {
        $id = (int) $query_id;

        $this->q_array[$id] = @mysql_fetch_array($query_id,MYSQL_ASSOC); // LINE 124
        return $this->q_array[$id]; 
    }

        else

        {
            return false;
        }
    }

I see this error:

Notice: Undefined offset: 1 in C:\xampp\htdocs\script\state.php on line 15

I change if ($ROW[1] == '') to if (isset($ROW[1])) but I see again error.

NOTE: i remove **MYSQL_ASSOC** from fetcharray function and fix error. i think my problem with fetcharray function how to fix this?

How I can Fix this error?

  • 写回答

2条回答 默认 最新

  • duanchui1279 2014-04-09 18:34
    关注

    This is not directly answer to your question BUT you implemented some kind of wrapper for old and deprecated functions.

    You should use MySQLi or PDO.

    In addition, you have inconsistent coding style. Once you use small letters for variables, another time you use capital letters.

    Instead of this:

    if ($ROW[1] == '') //line 15
              $ROW[1] = $ROW['subcategory'];
    

    you should use:

    if (!isset($ROW[1])) //line 15
              $ROW[1] = $ROW['subcategory'];
    

    or this:

    if (empty($ROW[1])) //line 15
              $ROW[1] = $ROW['subcategory'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题