2014-05-09 28 views
0

我想锻炼分页:SQL ORDER BY - 分页问题

if ($stmt = $mysqli->prepare("SELECT CVE_ORDEN_DET, ORDEN_TRABAJO_DET, NUM_MUESTRA, NUM_ESPECIMEN FROM colado_det WHERE CVE_OBRA = ? ORDER BY NUM_MUESTRA LIMIT ?, 30")) { 
$stmt->bind_param('ss', $obdt, $page);  
$stmt->execute(); 
$stmt->store_result(); 
$stmt->bind_result($id,$rf, $nm, $ne);  
while ($stmt->fetch()) {$arr_id[] = $id; $arr_rf[] = $rf;$arr_nm[] = $nm;$arr_ne[] = $ne; } 
} else {echo ("Error conectando a la base de datos"); ;} 

我认为代码是确定(这只是一个分页的一部分),问题是显示时结果,程序显示30行,第30行(至少一页)出现在下一页(开始时),这导致丢失一条记录。

当我使用ORDER BY DESC时,问题似乎并未出现,但使用降序不适合用户的需求,所以我需要找到是什么原因造成的,我该如何解决它。

我认为问题出在SQL,而不是PHP,有人可以帮助我确认这一点,或者给我一个关于可能导致这种情况的暗示吗?请!

欢迎提供一些文档。提前致谢。

编辑:

分页的第一部分:

if ($stmt = $mysqli->prepare("SELECT COUNT(*) FROM colado_det where fecha = ?")) { 
$stmt->bind_param('s', $asdfg);  
$stmt->execute(); 
$stmt->store_result(); 
$stmt->bind_result($counting); 
$stmt->fetch();  
    $all = $counting; 
    $page = round($page, 0); 
    $page = (30)*($page); 
    $counting = $counting/30; 
    $counting = ceil($counting); 
    if($counting<1){$counting=1;} 
} else {echo ("Error conectando a la base de datos");} 

页面变量临危经由$ _GET的数;这是一个稍后乘以[要显示的记录数]的数字,以获取记录编号。

我测试了它,它只使用ORDER BY子句时缺少一条记录。

+2

你是如何设置你的$页面变量?请添加该代码。 –

+0

当您更改页面时,$页面的值是多少?如果我想要前30行,它应该是0例如。如果我想要第二组30行,它应该是30. – skrilled

+1

它应该是这样的:'$ startRow =(($ page - 1)* 30);'..第1页将评估为0,你会得到'LIMIT 0,30',这将是0到29,第2页将评估为30,你会得到30到59等等...然后改变你的bind_param为'$ stmt-> bind_param('ss',$ obdt ,$ startRow);' –

回答

0
if ($stmt = $mysqli->prepare("SELECT COUNT(*) FROM colado_det where fecha = ?")) { 
$stmt->bind_param('s', $asdfg);  
$stmt->execute(); 
$stmt->store_result(); 
$stmt->bind_result($counting); 
$stmt->fetch();  
    $all = $counting; 
    $page = round($page, 0); 
    $page = (30)*($page); 
    $counting = $counting/30; 
    $counting = ceil($counting); 
    if($counting<1){$counting=1;} 
} else {echo ("Error conectando a la base de datos");} 

我真的不确定你要在这里完成什么,这是令人费解的。

应该是这样的;

// assuming $page is already set with the current page and you already have a variable holding $totalCount 
// but I will set them here for continuity; 
$page = 1; 
$totalCount = 538; // this is actually unnecessary for this, but is used to set up the display and make sure you don't run the query past the last page 
$startRow = (($page - 1) * 30); // page should ONLY hold the current page they're on 
if ($stmt = $mysqli->prepare("SELECT CVE_ORDEN_DET, ORDEN_TRABAJO_DET, NUM_MUESTRA, NUM_ESPECIMEN FROM colado_det WHERE CVE_OBRA = ? ORDER BY NUM_MUESTRA LIMIT ?, 30")) { 
$stmt->bind_param('ss', $obdt, $startRow);  
$stmt->execute(); 
$stmt->store_result(); 
$stmt->bind_result($id,$rf, $nm, $ne);  
while ($stmt->fetch()) {$arr_id[] = $id; $arr_rf[] = $rf;$arr_nm[] = $nm;$arr_ne[] = $ne; } 
} else {echo ("Error conectando a la base de datos"); ;} 
+0

许多感谢加里,它通过AJAX请求,页面变量只包含当前页面,我正在审查我的代码;但我真的认为它与SQL相关的不仅仅是PHP。有人救我请:( –

0

您正在使用字符串LIMIT

$stmt->bind_param('si', $obdt, $startRow); 
+0

好赶上..... –