PDO逐行获取数据库的下一行 [英] PDO fetching the next row of database row by row

查看:44
本文介绍了PDO逐行获取数据库的下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用PDO逐行从数据库中获取下一行? 当我单击下一步"按钮时,我正在尝试获取下一行数据 这是我的代码:

is there any way to fetch the next row from the database row by row using PDO?? i'm trying to fetch the next row of data when i click the next button and this is my code :

if($typeuser == 'admin'){
                        $dta = new PDO('mysql:host=localhost;dbname=laundry','root','');
                        $sta = $dta->prepare("SELECT * FROM konsumen");
                        $sta->execute();

                        $rowa = $sta->fetch();

                        $kode_pemesanan = $rowa['kode_pemesanan'];
                        $atas_nama = $rowa['atas_nama'];
                        $jmlhbaju = $rowa['jmlhbaju'];
                        $jmlhcelana = $rowa['jmlhcelana'];
                        $jmlhjaket = $rowa['jmlhjaket'];
                        $jmlhjas = $rowa['jmlhjas'];
                        $jmlhdress = $rowa['jmlhdress'];
                        $totalpesanan = $rowa['totalpesanan'];
                        $statuspesanan = $rowa['statuspesanan'];
                        $penguruspesanan = $rowa['penguruspesanan'];

                          print_r ("<table>
                                 <tr>
                                 <td>Kode Pemesanan</td><td>:</td><td>".$kode_pemesanan."</td></tr>
                                 <tr><td>Atas Nama</td><td>:</td><td>".$atas_nama."</td></tr>
                                 <tr><td>Jumlah Baju</td><td>:</td><td>".$jmlhbaju."</td></tr>
                                 <tr><td>Jumlah Celana</td><td>:</td><td>".$jmlhcelana."</td></tr>
                                 <tr><td>Jumlah Jaket</td><td>:</td><td>".$jmlhjaket."</td></tr>
                                 <tr><td>Jumlah Jas</td><td>:</td><td>".$jmlhjas."</td></tr>
                                 <tr><td>Jumlah Dress</td><td>:</td><td>".$jmlhdress."</td></tr>
                                 <tr><td>Pengurus Pesanan</td><td>:</td><td>".$penguruspesanan."</td></tr>
                                 <tr><td>Status Pesanan</td><td>:</td><td>".$statuspesanan."</td></tr>
                                 <tr><td>Total Pesanan</td><td>:</td><td>".$totalpesanan."</td>
                                 </tr></table>
                                 <br>
                                 <form method ='GET' action=''>
                                 <button type='submit' name='navigation' value='previous'>Previous</button>                      
                                 <button type='submit' name='navigation' value='next'>Next</button>
                                 </form>
                                 ");    



                        }

所以创建按钮后我被卡住了,我试图使用$ _GET ['navigation'],但是当第一次加载页面时,我得到了未定义的索引导航错误,单击后它向我显示结果,我认为问题出在提取上,所以我尝试做两次fetch(),它向我显示了下一行,但是效率不高,有什么办法让我使用按钮来提取下一行一排数据库的行?

so i got stuck after creating the button, i've tried to use the $_GET['navigation'] but when the page load in the first time i got error undefined index navigation, after i click then it show me the result, and i think the problem is about the fetch , so i tried to do fetch() two times , it shows me the next row but it isn't efficient, is there any way for me to use the button to fetch the next row of database's rows one by one?

推荐答案

您使用while循环,如:

while ($rowa = $sta->fetch()) {
    // Output contents
}

然后,您需要引入分页,以使其每页仅显示X行数,然后您可以通过单击下一个/上一个"链接查看下一批.

Then you need to introduce pagination so that it only display X number of rows per page, and then you can view the next batch by clicking next / prev links.

这是我对另一个有关分页的问题的深入回答

这篇关于PDO逐行获取数据库的下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆