问题
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>
");
}
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?
回答1:
You use a while loop like:
while ($rowa = $sta->fetch()) {
// Output contents
}
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.
Here's an in-depth answer I gave to another question about pagination
来源:https://stackoverflow.com/questions/37597890/pdo-fetching-the-next-row-of-database-row-by-row