dongmo1708 2014-03-24 21:05
浏览 50

PHP中未定义的变量和索引

I'm getting an undefined variable and undefined index notices that I thought isset() should have done away with. In this example I'm reading through a db consisting of individuals names and their companies. I want to separate out the unique companies, so I know how many times they are represented. Here's what I'm expecting it to do:

  1. Open the database, count the entries, load up the first row from the table.
  2. The first 'if' inside the 'for' loop looks to see if the $comp variable with a hash value of the company name exists. If it does, increment the count
  3. If $comp variable is not set, this is the first time I've seen this company so go to the 'else'.

However, isset() does not seem to be working because PHP is still throwing me notices about that line of code and that my variable and indexes aren't defined.

   $r1Q   = mysql_query("SELECT * FROM totals", $db);
   $r1C   = mysql_num_rows($r1Q);
   $r1    = mysql_fetch_array($r1Q);

    $title = "Companies Most Represented in DB";

    printf("
    <div class=\"content\">
    <h2>%s</h2>
    <center>
    <table border=1 bgcolor=#ffffff>
    <tr bgcolor=#cc9933>
        <th width=150>Company</th>
        <th width=50>Times</th>
    </tr>
    ", $title);

   // loop through 1 time for each entry in the db
   for($j=0;$j<$r1C;$j++) {
        $company = $r1[4];

        // have i seen this company before?
        if(isset($comp[$company])) {
            $cnt[$company]++;
            $cnt1[$company]++;
        }
        else {
            $comp[$company] = $r1[4];
            $cnt[$company]  = 1;
            $cnt1[$company] = 1;
        }
  • 写回答

4条回答 默认 最新

  • dongyuchen9276 2014-03-24 21:08
    关注

    You should initialize the arrays either:

    Add this before the loop:

     $comp = array();
     $cnt = array();
     $cnt1 = array();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化