php records navigation

0 votes

Code:

<?php 
$host='localhost'; 
$user='root'; 
$password='root'; 
$database='database'; 

$startindex=@$_REQUEST['seek']; 

$db=mysql_connect($host, $user, $password) 
or die ("Impossibile connettersi al server $host"); 

mysql_select_db($database, $db) 
or die ("Impossibile connettersi al database $database");



$query="SELECT * FROM ordini_master"; 
$dbResult=mysql_query($query, $db); 
$AffectedRows=mysql_affected_rows($db); 

mysql_data_seek($dbResult, $startindex); 

$row=mysql_fetch_row($dbResult); 

foreach($row as $k=>$v) 
{ 
    $myfield=mysql_fetch_field($dbResult, $k); 
    print($myfield->name . " : $v <br/>"); 
} 

mysql_free_result($dbResult); 
mysql_close($db); 

print("<br/>Seleziona il record<br/>"); 

for($index=0; $index<$AffectedRows; $index++) 
{ 
    print("<a href=\"{$_SERVER['PHP_SELF']}?seek=$index\" >" . 
    ($index+1) . "</a> "); 
} 
?> 

This code enables switching between a query's records, creating a page for every record in the database and displaying one record at a time. How can I change that code to page through records every ten? I want to display 10 records at a time and then make a page for the following.

Aug 8, 2022 in PHP by Kithuzzz
• 38,000 points
577 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

To obtain as many records as you like, use LIMIT.

Example:

SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15

Modified working code:

<?php 
$host='localhost'; 
$user='root'; 
$password='root'; 
$database='database'; 

$startindex=(isset($_REQUEST['seek']) ? $_REQUEST['seek'] : 0); 

$db=mysql_connect($host, $user, $password) 
or die ("Impossibile connettersi al server $host"); 

mysql_select_db($database, $db) 
or die ("Impossibile connettersi al database $database");

$queryCnt = "SELECT count(*) as cnt FROM ordini_master"; 
$CntRow=mysql_fetch_row(mysql_query($query, $db));
$CntData = $CntRow[0];

$step = 10;
$query="SELECT * FROM ordini_master LIMIT $startindex, $step"; 
$result=mysql_query($query, $db); 

while ($row = mysql_fetch_assoc($result)) {
    foreach($row as $k=>$v) { 
        $myfield=mysql_fetch_field($result, $k); 
        print($myfield->name . " : $v <br/>"); 
    } 
} 

mysql_free_result($result); 
mysql_close($db); 

print("<br/>Seleziona il record<br/>"); 

for($index=0; $index<$CntData; $index=$index+$step) { 
    print("<a href=\"{$_SERVER['PHP_SELF']}?seek=$index\" >" . 
    ($index+1) . "</a> "); 
} 

Utilize Tadman's data as well. The MySQL extension will no longer be maintained, so it's crucial to take action to prevent SQL injection and to utilize PDO or the mysqli extension.

I hope this helps you.

answered Aug 9, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In PHP

0 votes
1 answer

What are the vulnerability related to PHP Form?

Hii, The $_SERVER["PHP_SELF"] variable can be used by ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
3,402 views
0 votes
1 answer

How can we avoid my php form from hacking?

Hii @kartik, If you want to know php ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
2,715 views
0 votes
1 answer

How to Validate Form Data With PHP?

Hey @kartik, The first thing we will do ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
3,536 views
0 votes
1 answer

What is a Cookie? How to create Cookies With PHP?

A cookie is often used to identify ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
3,964 views
0 votes
1 answer

How to validate E-mail and URL of Php form?

hey, The code below shows a simple way ...READ MORE

answered Feb 13, 2020 in PHP by manish
2,503 views
0 votes
1 answer

What is Php json?

Hii @kartik, JSON stands for JavaScript Object Notation, ...READ MORE

answered Feb 14, 2020 in PHP by Niroj
• 82,840 points
681 views
0 votes
0 answers

Creating a search form in PHP [duplicate]

I am working on a function where ...READ MORE

Jun 9, 2022 in PHP by Kichu
• 19,040 points
505 views
0 votes
0 answers

How to delete image form folder and database

With the code below, I'm displaying data ...READ MORE

Jul 26, 2022 in PHP by Kithuzzz
• 38,000 points
1,497 views
0 votes
0 answers

How to fetch specific data from MySQL database to my PHP table?

I want to get data from the ...READ MORE

Jul 28, 2022 in PHP by Kithuzzz
• 38,000 points
3,656 views
0 votes
0 answers

How can I prevent SQL injection in PHP?

The programme  becomes vulnerable to SQL injection ...READ MORE

Jul 28, 2022 in PHP by Kithuzzz
• 38,000 points
595 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP