SELECT COUNT and Undefined offset issue



我在打印下面这个SELECT COUNT(*)页面的名字和姓氏时遇到问题。如果我删除了写着"'.$row[6].'..$row[7]."的代码,页面运行良好,只打印所选用户的计数点行,而不打印他们的名字和姓氏。如果我不删除这个代码,我会收到下面的消息:

*脚本"database.php"第177行出错:未定义的偏移量:6日期/时间:3-8-2012 12:27:03*

        <?php # index.php
        // Include the configuration file for error management and such.
        require_once ('./includes/config.inc.php');
        // Set the page title and include the HTML header.
        $page_title = 'Individual Member Transactions';
        include ('includes/header_admin_user.html');
        // If no dealer_code variable exists, redirect the user.
        if (!isset($_SESSION['admin_int_id'])) {
           // Start defining the URL.
           $url = 'http://' . $_SERVER['HTTP_HOST']
            . dirname($_SERVER['PHP_SELF']);
           // Check for a trailing slash.
           if ((substr($url, -1) == '/') OR (substr($url, -1) == '\') ) {
                $url = substr ($url, 0, -1); // Chop off the slash.
           }
           // Add the page.
           $url .= '/login.php'; 
        ob_end_clean(); // Delete the buffer.
        header("Location: $url"); 
        exit(); // Quit the script.
        }
        // Check for a valid user ID, through GET or POST.
        if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) )
           { // Accessed through view_users.php   
            $id = $_GET['id'];
        } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) )
           { // Form has been submitted.   
            $id = $_POST['id'];
        } else { // No valid ID, kill the script.
            echo '<h1 id="mainhead">Page Error</h1>
            <p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
            include ('./includes/header_admin_user.html'); 
            exit();
        }
        ?>
        <h1 id="mainhead">Points Transactions</h1>
          <div id="sidebar">
              <div id="statusbar">
            <p><span class="statusbar_highlight">Name:</span><br />
            <?php echo " {$_SESSION['adminfirstname']} " . " {$_SESSION['adminsurname']}<br> ";?></p>
            <p><span class="statusbar_highlight">Status:</span><br />
            <?php echo " {$_SESSION['adminstatus']} ";?></p>
            <p><span class="statusbar_highlight">Employer:</span><br />
            <?php echo " {$_SESSION['adminemployer']} ";?></p>      
            </div>
          </div>
          <div id="maincontent_results">
        <?php
        require_once ('database.php'); // Connect to the db.
        // Number of records to show per page:
        $display = 1000;
        // Determine how many pages there are. 
        if (isset($_GET['np'])) { // Already been determined.
            $num_pages = $_GET['np'];
        } else { // Need to determine.
            // Count the number of records
            $query = "SELECT COUNT(*) FROM tally_point, users WHERE tally_point.users_id = users.users_id ORDER BY tally_points_entry_date DESC";   
            $result = @mysql_query ($query);
            $row = @mysql_fetch_array ($result, MYSQL_NUM);
            $num_records = $row[0];
            // Calculate the number of pages.
            if ($num_records > $display) { // More than 1 page.
               $num_pages = ceil($num_records/$display);
            } else {
               $num_pages = 1;
            }
        } // End of np IF.
        // Determine where in the database to start returning results.
        if (isset($_GET['s'])) {
            $start = $_GET['s'];
        } else {
            $start = 0;
        }
        // Default column links.
        $link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
        $link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
        $link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
        // Determine the sorting order.
        if (isset($_GET['sort'])) {
            // Use existing sorting order.
            switch ($_GET['sort']) {
               case 'lna':
                  $order_by = 'tally_points_in ASC';
                  $link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
                  break;
               case 'lnd':
                  $order_by = 'tally_points_in DESC';
                  $link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
                  break;
               case 'fna':
                  $order_by = 'total ASC';
                  $link2 = "{$_SERVER['PHP_SELF']}?sort=fnd";
                  break;
               case 'fnd':
                  $order_by = 'total DESC';
                  $link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
                  break;
               case 'dra':
                  $order_by = 'tally_points_entry_date ASC';
                  $link3 = "{$_SERVER['PHP_SELF']}?sort=drd";
                  break;
               case 'drd':
                  $order_by = 'tally_points_entry_date DESC';
                  $link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
                  break;      
               default:
                  $order_by = 'tally_points_entry_date DESC';
                  break;
            }
            // $sort will be appended to the pagination links.
            $sort = $_GET['sort'];
        } else { // Use the default sorting order.
            $order_by = 'tally_points_entry_date DESC';
            $sort = 'dra';
        }
        // Select tally rows for the selected user and the users details
        $query =   "SELECT ta.tally_points_in, ta.order_id, ta.total, DATE_FORMAT(ta.tally_points_entry_date, '%d-%m-%Y') AS dr, ta.users_id, us.users_id, us.users_first_name, us.users_surname 
                    FROM tally_point AS ta, users AS us
                    WHERE ta.users_id=$id 
                    AND us.users_id = ta.users_id
                    ORDER BY
        ".$order_by." LIMIT ".$start.", ".$display;
        $result = @mysql_query ($query); 
        // Table header.
        echo ' ' . $row[6] . ' ' . $row[7] . '
        <table width="400" cellspacing="1" cellpadding="7">
        <tr class="top">
            <td align="left"><b><a href="' . $link2 . '">Date</a></b></td>
            <td align="center"><b><a href="' . $link3 . '">Credit</a></b></td>
            <td align="center"><b>Debit</b></td>
            <td align="center"><b>Description</b></td>
        </tr>
        ';
        // Fetch and print all the transactions.
        $bg = '#ffffff'; // Set the background color.
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $pointsitem = $row['order_id'];
            $bg = ($bg=='#eaeced' ? '#ffffff' : '#eaeced'); // Switch the background color.   
            //$entries = floor($row['ltd_entry_amount']/200);
            echo '<tr bgcolor="' . $bg . '">';
            echo '<td align="left">' . $row['dr'] . '</td>';
            echo  '<td align="center"><strong>' . $row['tally_points_in'] . '</strong></td> ';
            echo  '<td align="center">' . $row['total'] . '</td>';
        // products the footer, close the table, and the form.
        $str = '<td align="center">';
        if($pointsitem > '0') {
         $str .='<strong><a href="view-ind-order.php?id=' . $pointsitem . '">Order Details</a></strong></td></tr>';
        }
        else {
         $str .='Monthly Points Update</td></tr>'; 
        }   
        echo $str;  
            }
        echo '</table>';
        mysql_free_result ($result); // Free up the resources.  
        mysql_close(); // Close the database connection.
        // Make the links to other pages, if necessary.
        if ($num_pages > 1) {
            echo '<br /><p>';
            // Determine what page the script is on.
            $current_page = ($start/$display) + 1;
            // If it's not the first page, make a Previous button.
            if ($current_page != 1) {
               echo '<a href="view_points_2.php?s=' . ($start - $display) . '&np=' .
                 $num_pages . '&sort=' . $sort .'">Previous</a> ';   
            }
            // Make all the numbered pages.
            for ($i = 1; $i <= $num_pages; $i++) {
               if ($i != $current_page) {
                  echo '<a href="view_points_2.php?s=' . (($display * ($i - 1))) . 
                    '&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> ';   
               } else {
                  echo $i . ' ';
               }
            }
            // If it's not the last page, make a Next button.
            if ($current_page != $num_pages) {
               echo '<a href="view_points_2.php?s=' . ($start + $display) . '&np=' . 
                 $num_pages . '&sort=' . $sort .'">Next</a> ';   
            }
            echo '</p>';
        }  // End of links section.
        ?>
        <br class="clearboth" />
        </div>
        </div>
        </div>
        <?php
        include ('./includes/footer_admin_user.html'); // Include the HTML footer.
        ?>

您需要执行以下操作:

$row = @mysql_fetch_array ($result, MYSQL_NUM);

在错误之前的行上。

现在它给你带来了一个错误,因为你上次设置$row是在顶部的这段代码中:

// Count the number of records
$query = "SELECT COUNT(*) FROM tally_point, users WHERE tally_point.users_id = users.users_id ORDER BY tally_points_entry_date DESC";   
$result = @mysql_query ($query);
$row = @mysql_fetch_array ($result, MYSQL_NUM);

在这种情况下,$row仅具有一个元素($row[0])。

最新更新