dongyi6195 2017-05-15 14:50
浏览 200
已采纳

数据库查询表选择从第二行开始,而不是第一行

Basically im using html, php, Smarty.

When i loop my results to display database table rows it ignores the first row of my database table.

So if i have a table with 3 rows (product_id = 1, product_id = 2, product_id = 3), it displays only product_id '2' and '3'.

I want to be able to display the first row - (product_id = 1)

.PHP

<?php

$new = ['product_id','product_category','product_price','product_quantity','product_about','product_color'];

//Database connection
 $db = mysqli_connect('xxx','xxx','xxx','xxx')
 or die('Error connecting to MySQL server.');

//access Smarty template engine
require_once('Smarty-3.1.30/libs/Smarty.class.php');

$smarty = new Smarty();
$smarty->template_dir = 'views';
$smarty->compile_dir = 'tmp';


//query product page
$query = "SELECT * FROM cs_shop";
mysqli_query($db, $query) or die('Error querying database.');

$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);


//query an array of products
$rows = array();

 //loop start
 while ($row = mysqli_fetch_array($result)) {
    $rows[] = array(
        'product_id' => $row['product_id'],
        'product_category' => $row['product_category'],
        'product_price' => $row['product_price'],
        'product_quantity' => $row['product_quantity'],
        'product_about' => $row['product_about'],
        'product_color' => $row['product_color']
    );
}

//db collect data
$smarty->assign('row', $rows); 
//template
$smarty->display('index.tpl');

mysqli_close($db);

?>

.TPL

<div class="test divider">

<ul>
  {foreach from=$row item="item"}
  <li>{$item['product_id']}</li>
  {/foreach}
</ul>

</div>

Results

2
3

Now how to display the missing id.. im new to this so any tips or help of any kind is much appreciated. Thanks in advance.

Expected Result

1
2
3
  • 写回答

1条回答 默认 最新

  • duanbeng6709 2017-05-15 14:51
    关注

    Remove the following line right after your query $result = mysqli_query($db, $query);:

    $row = mysqli_fetch_array($result); // REMOVE ME
    

    It retrieves the first result, then you don't do anything with it before it enters your while loop and gets the next one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么