Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Tuesday, April 8, 2014

SQL Server - Result paganation in sql server


A lot of SQL Server database users at some time faced a big obstacle, especially web sites programmers when need to read data from the database in case of a partial data was a fairly large size. After a long and arduous journey made in the search for the best method that can solve the problem very haunting, I've experimenting with dozens of ways that were not satisfactory at first, especially if the data is very large size.

In fact,i did't invent magic solution, but I found the best located after many modifications and tests, all web solutions was talked about using  these methods (ROW_NUMBER() , TOP Rows ,CTE and Temp tables) in sql server 2005 and older versions wich i prefer using TOP method to done this job , because it's flexible and no limitation or conflicts for using TOP with your query criteria's and orders data results .


Using TOP IN SQL Server 2005
i prefer using sql stored procedure with parameters like @page_num , @page_size and @query_type that i use @query type to switch between counting results and query reaults it self .
Create PROCEDURE [dbo].[GetArchive]
(@username NVARCHAR(100),@page_num int,@page_size int,@proc_type nvarchar(20))AS
BEGIN


declare @page_count int ,@StartRow int , @EndRow int ,@sql nvarchar(max)
 

if (@proc_type='get_count')
begin
 set @sql = 'SELECT Count(*) as AllCount FROM Archive where id > 0'


end
else
begin
 set @sql = 'SELECT TOP ' + cast(@page_size as nvarchar(100)) 
 set @sql = @sql + ' Name,Mobile,Email,[Datetime] '
 set @sql = @sql +' FROM Archive '
 set @sql = @sql + 'WHERE ID NOT IN(SELECT TOP ('+cast(@page_size as nvarchar(100))+' * ('+cast(@page_num as nvarchar(100))+' - 1)) ID FROM Archive where id > 0 AND Username ='''+@username+''' order by id desc) '
end
set @sql = @sql + ' and username = '''+@username +'''';


if (@proc_type<>'get_count')
   set @sql = @sql + ' order by id desc '




exec (@sql)

 

END
Executing procedure will generate sql query like this
SELECT TOP 10 Name,Mobile,Email,[Datetime] FROM Archive WHERE ID NOT IN(SELECT TOP (10 * (2 - 1)) ID FROM Archive where id > 0 AND Username ='User' order by id desc)  and username = 'User' order by id desc 
Paganation IN SQL Server 2012
Sql server 2012 intreduce new keyword to do this job in easy and fast way like any other databsaes especially LIMIT in Mysql, using OFFSET ended this long conflict by add this keyword to your query
SELECT
     *
FROM  [dbo].[Archive]
ORDER BY id
OFFSET 10 ROWS
FETCH NEXT 20 ROWS ONLY

Sunday, April 6, 2014

PHP -MySQLCrank



MySQLCrank

Provides a number of functions to make it easier to work with MySQL databases, without using a lot of mysqli_xxxx functions, making it easier to switch to a different database. Supports SELECT, INSERT, UPDATE, and DELETE queries only, first because of security reasons, second - other queries are just not really needed for web development. 

Using MySqli_ extension (as described in php.net):

The mysqli extension, or as it is sometimes known, the MySQL improved extension, was developed to take advantage of new features found in MySQL systems versions 4.1.3 and newer. The mysqli extension is included with PHP versions 5 and later. The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:
  • Object-oriented interface
  • Support for Prepared Statements
  • Support for Multiple Statements
  • Support for Transactions

Supporting Parameters :

Through the support of the parameters you can use the various operations on the database in the same format, you can only change the type of operation required.

Classes

  • TDatabase
  • Includes all database operations function like SELECT , INSERT , UPDATE and DELETE .
  • TSQL
  • Includes structure of Parameters.

Setup database configuration

from TDatabase Class find these line and configure it as your database config.

$this->db_host "__SERVER_" ;$this->db_username "__DATABASE_USERNAME__";$this->db_password "__DATABASE_PASSWORD__";$this->db_database "__DATABASE_NAME__";?>

Basic Usage

Creating new instance of TDatabase class .

require_once(
"includes/TDatabase.php");$db = new TDatabase() ; /* Create new instance */
unset(
$db);  /* unset when finish using it. */
?>

Basic Select

Using Basic Select operation without any conditions.

$db = new TDatabase() ;$result $db->select("movies") ;
unset(
$db);?>

Select with where clause

Using Basic Select operation without any conditions

$db = new TDatabase() ;$db->where("movie_year","2013","i") ;$db->where("movie_rate","8","i") ;$result $db->select("movies") ;
unset(
$db);?>

where clause



/* where($name,$value,$type,$operand="=",$nextCondition="and") */
$db = new TDatabase() ;$db->where("movie_year","2013","i") ;$db->where("movie_rate","8","i") ;$result $db->select("movies") ;
unset(
$db);?>

Select with where and like operand



/* where($name,$value,$type,$operand="=",$nextCondition="and") */
$db = new TDatabase() ;$db->where("movie_name","Trek","s","like") ; $result $db->select("movies") ;
unset(
$db);?>

Select count for paging results



$db = new TDatabase() ;$db->where("movie_name","Trek","s","like") ;$resultCount $db->selectCount("movies") ; /* get count for same criterias*/
$result $db->select("movies") ;
unset(
$db);?>

Select and Limits



$db = new TDatabase() ;$db->limit(0,10);$result $db->select("movies") ;
unset(
$db);?>

Select and Order



$db = new TDatabase() ;$db->order("order by id desc");$result $db->select("movies") ;
unset(
$db);?>

Select with specified Columns

In case parameters added in select mode ,will be considered as columns must show only.

$db = new TDatabase() ;$db->addParameter("movie_rate","","i"); /* Parameters in select clause used as columns  */
$db->addParameter("movie_year","","i");$db->order("order by id desc");$result $db->select("movies") ;
unset(
$db);?>

Insert Operation



$db = new TDatabase() ;$db->addParameter("movie_name","Frozen (2013)","s");$db->addParameter("movie_rate","8","i");$db->addParameter("movie_year","2013","i");$db->addParameter("movie_story","Movie Story Here","s");$rows_affected $db->insert("movies") ;
unset(
$db);?>

Insert if not found and delete if found

An example of how easy to execute multi operations at the same time.

$db = new TDatabase() ;$db->addParameter("movie_name","Movie name","s");$db->addParameter("movie_rate","9","i");$db->addParameter("movie_year","2014","i");$db->where("movie_name","Movie name","s") ;

if(
$db->selectCount("movies")<=0)$db->insert("movies");
else
$db->delete("movies");

unset(
$db);?>

Update Operation with where



$db = new TDatabase() ;$db->addParameter("movie_rate","8","i");$db->addParameter("movie_year","2013","i");$db->where("id","1","i") ;$rows_affected $db->update("movies") ;
unset(
$db);?>

Delete Operation with where



$db = new TDatabase() ;$db->where("id","1","i") ; $rows_affected $db->delete("movies") ;
unset(
$db);?>

Safe Mode

You can activate safe mode to avoid overall effect on the records in operations (delete, modify) ,in this mode you must add one criteria at least .

$db = new TDatabase() ;$db->setSafeMode(true);$db->delete("movies") ;
unset(
$db);?>

Debug Query

You can see the query executed in database by calling $db->getLastQuery();

$db = new TDatabase() ;$db->select("movies") ;
echo  
$db->getLastQuery();
unset(
$db);?>

Download now ...