Rishi Rishi - 10 months ago 54
PHP Question

PHP : get data from database

I am making a web service in php.

I want to have data from database in pagination manner.

eg : I should have 10 records based on event_id which i am passing in url along with page_number i.e 10 records on 1st page , another 10 records on 2nd page, and so on.

I am very new to web service.



<?php
http://localhost/Mobile%20Webservice/customAppEngine/faq.php?Faq_request={"faq_event_id":"302","page_number":"1"}

if(isset($_REQUEST["Faq_request"]) && trim($_REQUEST["Faq_request"]) != '') {
$faq_details_Request = $_REQUEST["Faq_request"];
// $page_number = 1;
$faqeventdetails_o = json_decode($faq_details_Request);


if( isset($faqeventdetails_o->faq_event_id))
{
// event_id
$event_id = $faqeventdetails_o->faq_event_id;


if(trim($event_id)==''){
die($msgobj->customFailMessage("Please provide Event Id"));
}

$userQuery="SELECT * FROM `faq` WHERE faq_event_id=$event_id";

$u=mysql_query($userQuery);
$number=mysql_num_rows($u);

/* if($number>0){
$query="DELETE FROM `agendaLikes` WHERE event_id=$event_id and user_id=$user_id
and agenda_id=$agenda_id";
mysql_query($query);
mysql_query("UPDATE agenda SET agendaLikes=agendaLikes-1 where agenda_id=$agenda_id");


$arr['isLike']=0;
}else{
mysql_query("INSERT INTO `agendaLikes`(agenda_id,user_id,event_id) VALUES($agenda_id,$user_id,$event_id)");
$arr['isLike']=1;
mysql_query("UPDATE agenda SET agendaLikes=agendaLikes+1 where agenda_id=$agenda_id");
}*/

$arr['status']=$msgobj->success;

echo json_encode($u);

}
else {
die($msgobj->customFailMessage($msgobj->missingparamater));
}
}
else {
die($msgobj->customFailMessage($msgobj->invalidparamater));
}





based on
faq_event_id
i should get data.

when i hit in url then nothing is displayed.

where m I lacking?

Thank You.

Answer Source

Use Offset And Limit in your sql query let's say you want 10 records for each page then you need to pass page parameter. so for Example

$page = 2; //parameter pass by get or post method $limit = 10 //our limit to get data per page $offset = $page*$limit; //calculate your offset

Then your sql will look like

$userQuery="SELECT * FROM faq WHERE faq_event_id=$event_id ORDER BY id LIMIT $limit OFFSET $offeset";

Hope this works for you.