Using keyword LIMIT in MySQL - php

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("data_wis1") or die(mysql_error());
if(isset($_GET["id"])) {
$id = $_GET["id"];
$sql = "DELETE FROM info WHERE ID = '".$id."'";
mysql_query($sql) or die(mysql_error());
}
if(isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page = 1;
}
$start_from = ($page-1) * 1;
$sql = "SELECT * FROM info LIMIT 0, 1";
$query = mysql_query($sql) or die(mysql_error());
?>
<html>
<table>
<?php
for($i = 0; $i <mysql_num_rows($query); $i++) {
$id = mysql_result($query, $i, "ID");
$caseStatus = mysql_result($query, $i, "Case_Status");
echo "<tr>";
echo "<td>".$id."</td>";
echo "<td>".$caseStatus."</td>";
echo "<td><a href='del.php?id=".$id."'><input type='button' value='Delete'></a></td>";
echo "</tr>";
}
?>
</table>
<?php
$sql = "SELECT COUNT(ID) FROM info";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_row($query);
$total_records = $row[0];
$total_pages = ceil($total_records / 1);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='del.php?page=".$i."'>".$i."</a> ";
}
?>
</html>
My problem is that whenever i click page 2,3,4... the display doesn't change. Page 2,3,4 gets the display of page 1. It should be like this. For example, page 1 should display ID = 1 and case status = open. Page 2 should display ID = 2 and case status = close and so on.

You never used $start_from in anywhere. I think $sql = "SELECT * FROM info LIMIT '".$start_from."', 1"; is the answer

Related

php pagination code is not working

Can anyone point me out why my pagination is not working, What I am doing wrong here ? Working on it for a long time. my url generates like : http://localhost/medapp/admin/medorder.php?page=%209.
<?php
//pagination
$perpage = 3;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1)*$perpage;
//pagination
$medorder = "SELECT * FROM `medorder` WHERE status='1' order by ID desc";
$result = $db->select($medorder);
if($result){
$i=0;
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$i++."</td>";
echo "<td>".$row["uid"]."</td>";
echo "<td>".$row["fullname"]."</td>";
echo "</tr>";
}
// pagination
$query = "select * from medorder";
$result = $db->select($query);
$total_rows = mysqli_num_rows($result);
$total_pages = ceil($total_rows/$perpage);
echo "<span class='pagination'><a href='medorder.php?page=1'>".'First Page'."</a>";
for ($i=1; $i <= $total_pages; $i++) {
echo "<a href='medorder.php?page=".$i."'>".$i."</a>"; }
echo "<a href='medorder.php?page=$total_pages'>".'Last Page'."</a></span>";
//pagination
}
?>
Change your query like this by using LIMIT and OFFSET
$medorder = "SELECT * FROM `medorder` WHERE status='1' order by ID desc LIMIT $start_from,$perpage"; //
in this following line, you have a space before the $i variable:
echo "<a href='medorder.php?page= ".$i."'>".$i."</a>";
should be
echo "<a href='medorder.php?page=".$i."'>".$i."</a>";

PHP pagination script not work for search

I used this code in my all pages. Everywhere this code work well. In case of my search page this code count all page by search words but when I click next(2nd or 3rd) page, its cannot display any item. That means sql query for 2nd or 3rd or any others pages not worked. No error also. Pagination page url goes well also.
My Search code:
if(isset($_POST['searchword'])){
$w = mysqli_real_escape_string($dbh, $_POST['searchword']);
$q = strip_tags($w);
$q = trim ($w);
$limit = 12;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM search_data WHERE MATCH(detail) AGAINST('+$q*' IN BOOLEAN MODE) ORDER BY id ASC LIMIT $start_from, $limit";
$execute = $dbh->query("$sql");
$rowcount = $execute->num_rows ;
if ($rowcount > 0 ) {
$row = $dbh->query($sql) ;
while ($row = $execute->fetch_assoc()) {
$detail = $row['detail'];
$url = $row['url'];
echo $detail;
}
}
}
//Pagination
$sql = "SELECT COUNT(id) FROM search_data WHERE MATCH(detail) AGAINST('+$q*' IN BOOLEAN MODE)";
$result = mysqli_query($dbh,$sql);
$row = mysqli_fetch_row($result);
$total_records = $row[0];
if($total_records > 0){$total_pages = ceil($total_records / $limit); }
$pagLink = "<ul class='pagination'>";
if(!empty($total_pages)){for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li><a href='".$thispage."?page=".$i."'>".$i."</a> </li>";
};
echo $pagLink . "</ul>";
}

Passing query to next page (pagination)

Hi what I'm trying to do is make a pagination for the whole and gives also a chance the user to search for a name and returns a paginated result.
Here is my code
<form method = "POST">
<td>
Search:<input name="search_name" type="text" id="t_searchkey" style="width:35%;" placeholder = "Name">
<input type="submit" name="b_find" id="b_find" title="Find" value = "Find">
</td>
PHP code:
<?php
if(!isset($_POST['b_find']))
{
$query = "SELECT * FROM reginformation
WHERE deleted = 0";
$search_name = "";
}
if(isset($_POST['b_find']))
{
$search_name = trim($_POST['search_name']);
$query = "SELECT * FROM reginformation WHERE name = '$search_name' AND deleted = 0 ";
}
?>
<?php
$result = mysql_query($query) or die(mysql_error());
?>
<?php
$num_rec_per_page=5;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
if (isset($_GET["search"]))
{
$search_name = $_GET['search'];
}
$start_from = ($page-1) * $num_rec_per_page;
$query2 =$query . " LIMIT $start_from, $num_rec_per_page";
echo "query2 $query2";
$rs_result = mysql_query ($query2); //run the query
while ($row = mysql_fetch_assoc($rs_result)) {
echo "<tr onClick =window.location='infodetailsresp.php?id=$row[regID]'><td>$row[name]</td><td>$row[emailadd]</td><td>$row[contactno]</td><td>$row[event]</td><td>$row[date_register]</td></tr>";
};
//$sql = "SELECT * FROM reginformation";
$rs_result = mysql_query($query); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
?>
<?php
echo "<a href='reglistresp.php?page=1&search=$search_name'>".'|< '."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='reglistresp.php?page=".$i."&search=$search_name'>". $i ."</a> ";
};
echo "<a href='reglistresp.php?page=$total_pages&search=$search_name'>".' >| '."</a> "; // Goto last page
?>
What happens when I click the next page it gives me the query in the
if(!isset($_POST['b_find']))
So what should be changed so I can get my desired query to the next page?

Undefined offset: 0 error

I'm having this error on my code
Notice: Undefined offset: 0 in C:\xampp\htdocs\rekmovie\paginator.php on line 29
here is the full code
<?php
$con=mysql_connect("localhost","root","");
$conn=mysql_select_db('rektechnologies');
if (!$con)
{
die('Could not connect: ' . mysql_error());
} // include your code to connect to DB.
if (!empty($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20;
$sql = "select * from videos order by id desc LIMIT $start_from, 10";
$rs_result = mysql_query($sql,$con);
?>
<table>
<tr><td>Name</td><td>Phone</td></tr>
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr>
<td><? echo $row["path"]; ?></td>
<td><? echo $row["description"]; ?></td>
</tr>
<?php
};
?>
<?php
$sql = "SELECT COUNT(*) FROM videos";
$rs_result = mysql_query($sql,$con);
$row = mysql_fetch_assoc($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 10);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='paginator.php?page=".$i."'>".$i."</a> ";
};
?>
</table>
can anyone help me with the error
Change
$row = mysql_fetch_assoc($rs_result);
to:
$row = mysql_fetch_row($rs_result);
mysql_fetch_assoc returns an associative array, but you're trying to access $row[0], which expects an indexed array.
You are using mysql_fetch_assoc, but then trying to access the column name by integer
$row = mysql_fetch_assoc($rs_result);
$total_records = $row[0];
use either
$row = mysql_fetch_assoc($rs_result);
$total_records = $row['COUNT(*)'];
or
$row = mysql_fetch_array($rs_result);
$total_records = $row[0];
change the query to SELECT COUNT(*) as some_alias FROM videos
and then use $total_records = $row['some_alias'];

i got error while coding of pagination in php5?

i am writing code of pagination & i got an error.
this is my code :
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("Admin") or die(mysql_error());
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * 2;
$sql = "SELECT * FROM events ORDER BY event ASC LIMIT $start_from, 2";
$result = mysql_query ($sql) or die(mysql_error());
$num=mysql_numrows($result);
$x=0;
?>
<table>
<tr><td>Event</td><td>Types</td></tr>
<?php
while ($x<$num) {
$row1 = mysql_result($result,$x,'event');
$row2 = mysql_result($result,$x,'types');
?>
<tr>
<td><? echo $row1; ?></td>
<td><? echo $row2; ?></td>
</tr>
<?php
$x++;
}
?>
</table>
<?php
$sql = "SELECT COUNT(event) FROM events";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_row($result);
$total_records = $row[0];
$total_pages = ceil($total_records / 2);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
}
?>
now i got an error.
object not found
Note that instead of terrible while ($x<$num) you should use way more neat while($row=mysql_fetch_array($res))
so, make it
<?
$per_page = 2;
$page = 1;
if (isset($_GET['page'])) $page = $_GET['page'];
$start_from = ($page-1) * $per_page;
$sql = "SELECT * FROM events ORDER BY event ASC LIMIT $start_from, $per_page";
$res = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);
?>
<table>
<tr><td>Event</td><td>Types</td></tr>
<?php
while($row=mysql_fetch_array($res)) {
?>
<tr>
<td><? echo $row['event'] ?></td>
<td><? echo $row['types'] ?></td>
</tr>
<?php
}
?>
</table>
<?php
$sql = "SELECT COUNT(event) FROM events";
$res = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);
$row = mysql_fetch_row($res);
$total_records = $row[0];
$total_pages = ceil($total_records / $per_page);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
}
?>
mysql_query returns false - i.e. your query failed and you are passing a wrong argument to mysql_num_rows. I think you have a typo in the table name in the SQL query, it should be events instead. Generally, you should check if the query succeeded:
$result = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);

Categories