PHP - 分页 - 为什么我收到此错误:在MySQL结果索引14中找不到?

时间:2011-09-03 17:35:43

标签: php mysql pagination

我在分析从Mysql数据库中提取的一些结果时遇到了问题。

这是与分页相关的代码:

$max_results = 5; //Max listed query results

if(!isset($_GET['pg'])){ $pg = 1; } else { $pg = $_GET['pg']; }
$from = (($pg * $max_results) - $max_results);

/// START of MySQL results – Page numbering
    $space_separated = implode(" ", $keywords_array);
/// Count total
$totals = mysql_result(mysql_query("SELECT COUNT(id)
            FROM products
            WHERE MATCH (`desc`)
            AGAINST ('".$space_separated."' IN BOOLEAN MODE)
            ORDER BY `desc`
            LIMIT $from, $max_results "),0);
$total_pgs = ceil($totals / $max_results);
$tempVar = 0; //Used in the generation of order form

///Now we search for our search term, in the field the user specified 

// Page limiter & result builder    
$dataQuery = "SELECT id, `desc`, price1, pack, measure, quantity
              FROM products
              WHERE MATCH (`desc`)
              AGAINST ('".$space_separated."' IN BOOLEAN MODE)
              ORDER BY `desc`
              LIMIT $from, $max_results";
$result1 = mysql_query($dataQuery);
$num_sql = mysql_num_rows ($result1);
$data = mysql_query($dataQuery) or die(mysql_error());

$pageContent .= '
<div>
<p>Results: '.$totals.'</p>
<p>Viewing page '.$pg.' of '.$total_pgs.'</p>
</div>
<!-- end .aliLeft --></div>
';

// Build paginator
if($pg > 1){ $prev = ($pg - 1); // Previous Link
$paginator ='<a href="'.$_SERVER['PHP_SELF'].'?pg='.$prev.'">"Previous page</a>'; }
for($i = 1; $i <= $total_pgs; $i++){ /// Numbers
if(($pg) == $i) { $paginator .= "<i>$i</i> "; } else {
$paginator .='<a href="'.$_SERVER['PHP_SELF'].'?pg='.$i.'">$i</a> '; }}
if($pg < $total_pgs){ $next = ($pg + 1); // Next Link
$paginator .='<a href="'.$_SERVER['PHP_SELF'].'?pg='.$next.'">"Next page."</a>'; }

$pageContent .= '
<p>'.$paginator.'</p>
';

/// Display results
if ($num_sql > 0 ) {$i=0;
while ($i < $num_sql) {
$holsite = mysql_result($result1,$i,"holsite");
$pageContent .= ''.$holsite.'';
++$i;}}

$pageContent .= '
<p>'.$paginator.'</p>
';

我正在修改本教程:http://www.webdesign-4-beginners.co.uk/web-design-blog/2009/12/mysql-results-page-numbering/

我收到以下mysql错误5次(5是应该有的页数):

警告:mysql_result()[function.mysql-result]:在第459行的/path-to/orders-layout.php中的MySQL结果索引14中找不到holsite

有没有人知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的问题是您的查询

$dataQuery = "SELECT id, `desc`, price1, pack, measure, quantity
          FROM products
          WHERE MATCH (`desc`)
          AGAINST ('".$space_separated."' IN BOOLEAN MODE)
          ORDER BY `desc`
          LIMIT $from, $max_results";

不包含您​​在底部查找的列holsite的任何引用。 (mysql_result的第3个参数是字段名称)

$holsite = mysql_result($result1,$i,"holsite");

要么你没有在查询中提取holsite,要么在设置$ holsite时你得到了列的名称错误。